我正在尝试运行这样的代码:
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)
使用 copy_expert cursor method .
但是我收到了这个错误:
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.
我无法以 super 用户身份运行它,而且似乎不需要这样做,因为我没有接触任何真实文件。
最佳答案
COPY TO STDOUT
,将数据流式传输回客户端,以及COPY TO 'filename'
,写入服务器端文件(需要 super 用户权限)。
您的 COPY
语句在 STDOUT
关键字周围加了引号,导致它被解释为文件名。只需删除它们即可。
关于python - Psycopg copy_expert 方法 - 如何正确使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41616578/