相关疑难解决方法(0)

使用python将数据从csv复制到postgresql

我在Windows 7 64位.我有一个csv文件'data.csv'.我想通过python脚本将数据导入postgresql表'temp_unicommerce_status'.

我的剧本是:

import psycopg2
conn = psycopg2.connect("host='localhost' port='5432' dbname='Ekodev' user='bn_openerp' password='fa05844d'")
cur = conn.cursor()
cur.execute("""truncate table "meta".temp_unicommerce_status;""")
cur.execute("""Copy temp_unicommerce_status from 'C:\Users\n\Desktop\data.csv';""")
conn.commit()
conn.close()
Run Code Online (Sandbox Code Playgroud)

我收到了这个错误

Traceback (most recent call last):
  File "C:\Users\n\Documents\NetBeansProjects\Unicommerce_Status_Update\src\unicommerce_status_update.py", line 5, in <module>
cur.execute("""Copy temp_unicommerce_status from     'C:\\Users\\n\\Desktop\\data.csv';""")
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.
Run Code Online (Sandbox Code Playgroud)

python postgresql psycopg2 postgresql-copy

14
推荐指数
3
解决办法
3万
查看次数

不能用 Postgres 和 Python “COPY FROM”

作为主题,这是代码,没有错误消息,但没有插入数据。这是我的代码,谁能告诉我它有什么问题?

import psycopg2
import sys
import os
import glob 
import csv

#open the csv folder
dictfile='******'
os.chdir(dictfile)
total=[]
for file in glob.glob("*.csv"):
    total.append(file)
con = None
try: 
    con = psycopg2.connect(host='localhost',database='*****',user='postgres', password='*****') 
    cur = con.cursor()
    for i in range(0,1):   
        filename='/Users/Shared'+'/'+total[0]
        print filename
        #better move all files into shared folder
        x="copy public.crossref_sample from "+ "'"+filename+"'"+" DELIMITERS ',' CSV"
        print x
        cur.execute(x)  
except psycopg2.DatabaseError, e:
    print 'Error %s' % e    
    sys.exit(1) 
finally:
    if con:
        con.close()
Run Code Online (Sandbox Code Playgroud)

python postgresql import copy psycopg2

2
推荐指数
1
解决办法
1544
查看次数

标签 统计

postgresql ×2

psycopg2 ×2

python ×2

copy ×1

import ×1

postgresql-copy ×1