简体   繁体   中英

Psycopg2 - copy_expert permission denied error

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()

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM