Psycopg2 - copy_expert permission denied error

独自空忆成欢 提交于 2020-01-05 13:17:09

问题


I'm attempting to make the switch from Windows to ubuntu (am using 12.04 LTS) and am trying to use some of my old scripts to run my old databases. Previously I used postgresql and psycopg2 to maintain them and I am trying to do so again here.

My error is around importing a csv file to a table using the copy expert command.

Code is as follows:

#!/usr/bin/env python
import psycopg2 as psy
import sys

conn = psy.connect("dbname, user, host, password") # with the appropriate  values
curs = conn.cursor()

table = 'tablename' # a table with the appropriate columns etc
file = 'filename' # a csv file
SQL = "COPY %s FROM '%s' WITH CSV HEADERS" % (tablename, filename)

curs.copy_expert(SQL, sys.stdin) # Error occurs here

conn.commit()
curs.close()
conn.close()

The specific error which is occurring is as follows:

psycopg2.ProgrammingError: could not open file "filename" for reading: Permission denied

Any assistance would be greatly appreciated:

I am completely stuck and I believe it is due some quirk of how I've set up the database or the files.

Adding a simple read and print command using the csv module works fine as well (from the same script in fact) It will output all of the information from the csv file and then error out with the permission denied when attempting to import it to the database

import csv
f = open(filename, 'rb')
read = csv.reader(f, delimiter =',')
for row in read:
    print row
f.close()

回答1:


Try executing the command as the super user by using su or sudo and if this doesn't help, the other possiblity is that the location of the filename is out of bounds so I would try copying it to the desktop or your home directory or folder where you know you definitely have full permissions and see if this works.



来源:https://stackoverflow.com/questions/10065926/psycopg2-copy-expert-permission-denied-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!