我尝试使用python和psycopg2将CSV文件从文件夹复制到postgres表,我收到以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
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.
我还尝试在python环境中运行它:
constr = "dbname='db_name' user='user' host='localhost' password='pass'"
conn = psycopg2.connect(constr)
cur = conn.cursor()
sqlstr = "COPY test_2 FROM '/tmp/tmpJopiUG/downloaded_xls.csv' DELIMITER ',' CSV;"
cur.execute(sqlstr)
我仍然遇到上述错误。我试过\ copy命令,但这只适用于psql。为了能够通过我的python脚本执行此操作,有什么选择?
EDITED
看了@IljaEveilä提供的链接后,我尝试了这个:
cur.copy_from('/tmp/tmpJopiUG/downloaded_xls.csv', 'test_copy')
我收到错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: argument 1 must have both .read() and .readline() methods
我如何提供这些方法?
答案 0 :(得分:2)
尝试使用cursor.copy_expert()
:
constr = "dbname='db_name' user='user' host='localhost' password='pass'"
conn = psycopg2.connect(constr)
cur = conn.cursor()
sqlstr = "COPY test_2 FROM STDIN DELIMITER ',' CSV"
with open('/tmp/tmpJopiUG/downloaded_xls.csv') as f:
cur.copy_expert(sqlstr, f)
conn.commit()
您必须在python 中打开文件并将其传递给psycopg,然后psycopg将其转发到postgres的stdin。由于您使用的CSV
参数为COPY
,因此您必须使用自己传递COPY语句的专家版本。
答案 1 :(得分:0)
您也可以使用copy_from。参见下面的代码
with open('/tmp/tmpJopiUG/downloaded_xls.csv') as f:
cur.copy_from(f, table_name,sep=',')
conn.commit()