from typing import Union


def table_select(table_name: str, return_field: str='*',
                 search_field: Union[str, None]=None) -> str:
    """Return values from a Postgres table with a given search value.

    :param str table_name: name of table to search
    :param str return_field: field to return from table query (default: * \
        will return all table fields)
    :param str search_field: field to search for value in table (default: \
    None will return all table values)
    :returns: value corresponding to requested field
    :rtype: str

    Example:

    import psql

    cur = psql.connection('db', 'user', 'password')
    [cur.execute(psql.table_select('table', search_field='idx'), (x, )) \
     for x in values]
    """
    base_cmd = 'SELECT {} FROM {}'.format(return_field, table_name)

    if search_field:
        search_cmd = 'WHERE {}=%s'.format(search_field)
    else:
        search_cmd = ''

    return '{} {};'.format(base_cmd, search_cmd)