code: def psql_csv_run(sql_command, error_handler=None):
      
      csv_query = ('COPY ({query}) TO STDOUT WITH CSV HEADER;'
                   .format(query=sql_command))
  
      new_env = os.environ.copy()
      new_env.setdefault('PGOPTIONS', '')
      new_env["PGOPTIONS"] += ' --statement-timeout=0'
      psql_proc = popen_nonblock([PSQL_BIN, '-d', 'postgres', '--no-password',
                                  '--no-psqlrc', '-c', csv_query],
                                 stdout=PIPE,
                                 env=new_env)
      stdout = psql_proc.communicate()[0].decode('utf-8')
  
      if psql_proc.returncode != 0:
          if error_handler is not None:
              error_handler(psql_proc)
          else:
              assert error_handler is None
              raise UserException(
                  'could not csv-execute a query successfully via psql',
                  'Query was "{query}".'.format(sql_command),
                  'You may have to set some libpq environment '
                  'variables if you are sure the server is running.')
  
      # Previous code must raise any desired exceptions for non-zero
      # exit codes
      assert psql_proc.returncode == 0
  
      # Fake enough iterator interface to get a CSV Reader object
      # that works.
      return csv.reader(iter(stdout.strip().split('\n')))
docs: 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.