def psql_csv_run(sql_command, error_handler=None):
csv_query = ('<STR_LIT>'<EOL>.format(query=sql_command))<EOL>new_env = os.environ.copy()<EOL>new_env.setdefault('<STR_LIT>', '<STR_LIT>')<EOL>new_env["<STR_LIT>"] += '<STR_LIT>'<EOL>psql_proc = popen_nonblock([PSQL_BIN, '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT:-c>', csv_query],<EOL>stdout=PIPE,<EOL>env=new_env)<EOL>stdout = psql_proc.communicate()[<NUM_LIT:0>].decode('<STR_LIT:utf-8>')<EOL>if psql_proc.returncode != <NUM_LIT:0>:<EOL><INDENT>if error_handler is not None:<EOL><INDENT>error_handler(psql_proc)<EOL><DEDENT>else:<EOL><INDENT>assert error_handler is None<EOL>raise UserException(<EOL>'<STR_LIT>',<EOL>'<STR_LIT>'.format(sql_command),<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT><DEDENT>assert psql_proc.returncode == <NUM_LIT:0><EOL>return csv.reader(iter(stdout.strip().split('<STR_LIT:\n>')))<EOL>
Runs psql and returns a CSVReader object from the query

This CSVReader includes header names as the first record in all
situations.  The output is fully buffered into Python.