Gre*_*reg 1 python postgresql psycopg2 psycopg
我正在尝试运行这样的代码:
query = "copy (select email from my_table) TO 'STDOUT' WITH (FORMAT csv, DELIMITER '|', QUOTE '^', HEADER FALSE)"
out_file = StringIO()
cursor.copy_expert(query, out_file, size=8192)
Run Code Online (Sandbox Code Playgroud)
但我收到此错误:
Traceback (most recent call last):
File "etl/scripts/scratch.py", line 32, in <module>
cursor.copy_expert(query, out_file, size=8192)
psycopg2.ProgrammingError: must be superuser to COPY to or from a file
HINT: Anyone can COPY to stdout or from stdin. psql's \copy command also works for anyone.
Run Code Online (Sandbox Code Playgroud)
我无权以超级用户身份运行它,而且由于我没有接触任何实际文件,因此似乎不需要这样做。
有以下两种变体COPY TO
:
COPY TO STDOUT
,它将数据流回到客户端,以及COPY TO 'filename'
,它写入服务器端文件(需要超级用户特权)。您的COPY
语句在STDOUT
关键字两边加上引号,导致其被解释为文件名。只需删除它们。