我试图运行这样的代码:

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.

我无法以超级用户的身份运行它,似乎不需要这样做,因为我没有接触任何真正的文件。

最佳答案

two variants of COPY TO
COPY TO STDOUT,它将数据流传回客户端,并且
COPY TO 'filename',它写入服务器端文件(需要超级用户权限)。
COPY语句在STDOUT关键字周围有引号,导致它被解释为文件名。把它们拿走就行了。

关于python - Psycopg copy_expert方法-如何正确使用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41616578/

10-15 19:42