Psycopg2 - copy_expert 权限被拒绝错误 [英] Psycopg2 - copy_expert permission denied error

查看:82
本文介绍了Psycopg2 - copy_expert 权限被拒绝错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Windows 切换到 ubuntu(我使用的是 12.04 LTS)并尝试使用我的一些旧脚本来运行我的旧数据库.以前我使用 postgresql 和 psycopg2 来维护它们,我在这里再次尝试这样做.

我的错误是使用复制专家命令将 csv 文件导入到表中.

代码如下:

#!/usr/bin/env python导入 psycopg2 作为 psy导入系统conn = psy.connect("dbname, user, host, password") # 使用适当的值curs = conn.cursor()table = 'tablename' # 具有适当列等的表file = 'filename' # 一个 csv 文件SQL = "COPY %s FROM '%s' WITH CSV HEADERS" % (表名,文件名)curs.copy_expert(SQL, sys.stdin) # 这里发生错误conn.commit()curs.close()conn.close()

具体发生的错误如下:

<块引用><块引用>

psycopg2.ProgrammingError:无法打开文件filename"进行读取:权限被拒绝

非常感谢任何帮助:

我完全被卡住了,我相信这是由于我设置数据库或文件的方式有些奇怪.

使用 csv 模块添加一个简单的读取和打印命令也可以正常工作(实际上来自同一个脚本)它将输出 csv 文件中的所有信息,然后在尝试导入时因权限被拒绝而出错到数据库

导入csvf = 打开(文件名,'rb')read = csv.reader(f, delimiter =',')对于读取中的行:打印行f.close()

解决方案

尝试使用 susudo 以超级用户身份执行命令,如果没有帮助,另一种可能是文件名的位置越界,所以我会尝试将其复制到桌面或您的主目录或文件夹,您知道您肯定拥有完全权限,看看这是否有效.

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.

这篇关于Psycopg2 - copy_expert 权限被拒绝错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆