def psql_csv_run(sql_command, error_handler=None): """ 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. """ csv_query = ('COPY ({query}) TO STDOUT WITH CSV HEADER;' .format(query=sql_command)) psql_proc = popen_nonblock([PSQL_BIN, '-d', 'postgres', '-c', csv_query], stdout=PIPE) stdout = psql_proc.communicate()[0] 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')))
def psql_csv_run(sql_command, error_handler=None): """ 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. """ csv_query = ('COPY ({query}) TO STDOUT WITH CSV HEADER;'.format( query=sql_command)) psql_proc = popen_nonblock([PSQL_BIN, '-d', 'postgres', '-c', csv_query], stdout=PIPE) stdout = psql_proc.communicate()[0] 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')))
def run_pg_basebackup(cls, user, host, archive_directory): psql_proc = popen_nonblock([PG_BASEBACKUP_BIN, '--write-recovery-conf', '--format=plain', '-D', archive_directory, '--host', host, '--username', user, '--xlog-method=stream'], stdout=PIPE) stdout = psql_proc.communicate()[0].decode('utf-8') if psql_proc.returncode != 0: raise UserException("Could not run pg_basebackup: {stdout}" .format(stdout=stdout)) assert psql_proc.returncode == 0 return
def psql_csv_run(sql_command, error_handler=None): """ 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. """ 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")))