我正在尝试使用Python在文件系统中将postgres数据库表导出为csv文件,但是在运行复制查询时遇到了问题。
import psycopg2
import Config
class postgres_to_s3():
def __init__(self):
app_config = Config.Config
self.pg_conn = app_config.pg_conn
self.pg_cur = app_config.pg_cur
def unload_database_to_CSV(self):
query1 = '\COPY source_checksum TO /Users/Will/Downloads/output.csv WITH (FORMAT CSV, HEADER);'
with open ('/Users/Will/Downloads/output.csv', 'w') as f:
self.pg_cur.copy_expert(query1, f)
s1 = postgres_to_s3()
s1.unload_database_to_CSV()
我得到了错误:
psycopg2.ProgrammingError: syntax error at or near "\"
LINE 1: \COPY source_checksum TO /Users/Will/Downloads/output.csv
^
我能够在psql控制台上执行查询。我尝试使用双反斜杠,但仍然出现相同的错误。
编辑:following this thread我删除了反斜杠,但现在出现错误:
psycopg2.ProgrammingError: syntax error at or near "/"
LINE 1: COPY source_checksum TO /Users/Will/Downloads/output.csv
^