使用 python 将数据从 csv 复制到 postgresql
copy data from csv to postgresql using python
我在 windows 7 64 位。
我有一个 csv 文件 'data.csv'。
我想通过 python 脚本将数据导入 postgresql table '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()
我遇到了这个错误
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.
尝试以 root 用户身份执行相同的操作 - postgres。如果是 linux 系统,您可以更改文件的权限或将文件移动到 /tmp。问题是由于缺少从文件系统读取的凭据。
以下是相关 PostgreSQL 文档的摘录:带有文件名的 COPY 指示 PostgreSQL 服务器直接读取或写入文件。服务器必须可以访问该文件,并且必须从服务器的角度指定名称。当指定STDIN或STDOUT时,数据通过客户端和服务器之间的连接传输
这就是为什么对文件的 copy
命令仅限于 PostgreSQL 超级用户:文件必须存在于服务器上并且由服务器进程直接加载。
您应该改为使用:
cur.copy_from(r'C:\Users\n\Desktop\data.csv', temp_unicommerce_status)
如 this other answer 所建议,因为它在内部使用来自标准输入的 COPY
。
f = open(r'C:\Users\n\Desktop\data.csv', 'r')
cur.copy_from(f, temp_unicommerce_status, sep=',')
f.close()
文件必须作为对象传递。
由于您处理的是 csv 文件,因此有必要指定分隔符,因为默认为制表符
我解决这个问题的方法特别是使用 psychopg2 游标 class 函数 copy_expert(文档:http://initd.org/psycopg/docs/cursor.html)。 copy_expert 允许您使用 STDIN,因此无需为 postgres 用户授予超级用户权限。您对该文件的访问权限取决于客户端 (linux/windows/mac) 用户对该文件的访问权限
来自 Postgres COPY 文档 (https://www.postgresql.org/docs/current/static/sql-copy.html):
Do not confuse COPY with the psql instruction \copy. \copy invokes
COPY FROM STDIN or COPY TO STDOUT, and then fetches/stores the data in
a file accessible to the psql client. Thus, file accessibility and
access rights depend on the client rather than the server when \copy
is used.
您也可以严格保留访问 development_user 主文件夹和 App 文件夹的权限。
csv_file_name = '/home/user/some_file.csv'
sql = "COPY table_name FROM STDIN DELIMITER '|' CSV HEADER"
cursor.copy_expert(sql, open(csv_file_name, "r"))
您可以使用 d6tstack 使这变得简单
import d6tstack
import glob
c = d6tstack.combine_csv.CombinerCSV([r'C:\Users\n\Desktop\data.csv']) # single-file
c = d6tstack.combine_csv.CombinerCSV(glob.glob('*.csv')) # multi-file
c.to_psql_combine('postgresql+psycopg2://psqlusr:psqlpwdpsqlpwd@localhost/psqltest', 'tablename')
它还处理 data schema changes、create/append/replace table 并允许您使用 pandas.
预处理数据
我知道这个问题已经得到解答,但这是我的两分钱。我正在添加更多描述:
您可以使用cursor.copy_from
方法:
首先,您必须创建一个 table 列数与您的 csv 文件相同的文件。
示例:
我的 csv 看起来像这样:
Name, age , college , id_no , country , state , phone_no
demo_name 22 , bdsu , 1456 , demo_co , demo_da , 9894321_
先创建一个table:
import psycopg2
from psycopg2 import Error
connection = psycopg2.connect(user = "demo_user",
password = "demo_pass",
host = "127.0.0.1",
port = "5432",
database = "postgres")
cursor = connection.cursor()
create_table_query = '''CREATE TABLE data_set
(Name TEXT NOT NULL ,
age TEXT NOT NULL ,
college TEXT NOT NULL ,
id_no TEXT NOT NULL ,
country TEXT NOT NULL ,
state TEXT NOT NULL ,
phone_no TEXT NOT NULL);'''
cursor.execute(create_table_query)
connection.commit()
现在您可以在需要三个参数的地方简单地使用 cursor.copy_from :
first file object , second table_name , third sep type
您现在可以复制了:
f = open(r'final_data.csv', 'r')
cursor.copy_from(f, 'data_set', sep=',')
f.close()
完成
我将 post 我 运行 在尝试将 csv 文件复制到基于 linux 的系统上的数据库时遇到的一些错误....
这是一个示例 csv 文件:
Name Age Height
bob 23 59
tom 56 67
您必须安装库 psycopg2(即 pip install psycopg2 或 sudo apt install python3-psycopg2)
您必须在系统上安装 postgres 才能使用 psycopg2 (sudo apt install postgresql-server postgresql-contrib )
现在您必须创建一个数据库来存储 csv,除非您已经使用 pre-existing 数据库
设置了 postgres
使用 Postgres 命令复制 CSV
安装 postgres 后,它会创建一个默认用户帐户,让您可以访问 postgres 命令
切换到postgres账号问题:sudo -u postgres psql
通过发出以下命令访问提示:psql
#创建数据库的命令
创建数据库mytestdb;
#连接到数据库创建一个table
\连接我的测试数据库;
#create a table 具有相同的 csv 列名
创建 table 测试(姓名字符(50),年龄字符(50),身高字符(50));
#复制csv文件到table
用 csv header;
复制 mytestdb 'path/to/csv'
使用 PYTHON 复制 CSV
我 运行 将 CSV 文件复制到数据库的主要问题是我还没有创建数据库,但是这仍然可以通过 python 完成。
import psycopg2 #import the Postgres library
#connect to the database
conn = psycopg2.connect(host='localhost',
dbname='mytestdb',
user='postgres',
password='')
#create a cursor object
#cursor object is used to interact with the database
cur = conn.cursor()
#create table with same headers as csv file
cur.execute('''create table test(name char(50), age char(50), height char(50));''')
#open the csv file using python standard file I/O
#copy file into the table just created
f = open('file.csv','r')
cursor.copy_from(f, 'test', sep=',')
f.close()
#sample of code that worked for me
import psycopg2 #import the postgres library
#connect to the database
conn = psycopg2.connect(host='localhost',
dbname='database1',
user='postgres',
password='****',
port='****')
#create a cursor object
#cursor object is used to interact with the database
cur = conn.cursor()
#create table with same headers as csv file
cur.execute("CREATE TABLE IF NOT EXISTS test(**** text, **** float, **** float, ****
text)")
#open the csv file using python standard file I/O
#copy file into the table just created
with open('******.csv', 'r') as f:
next(f) # Skip the header row.
#f , <database name>, Comma-Seperated
cur.copy_from(f, '****', sep=',')
#Commit Changes
conn.commit()
#Close connection
conn.close()
f.close()
我在 windows 7 64 位。 我有一个 csv 文件 'data.csv'。 我想通过 python 脚本将数据导入 postgresql table '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()
我遇到了这个错误
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.
尝试以 root 用户身份执行相同的操作 - postgres。如果是 linux 系统,您可以更改文件的权限或将文件移动到 /tmp。问题是由于缺少从文件系统读取的凭据。
以下是相关 PostgreSQL 文档的摘录:带有文件名的 COPY 指示 PostgreSQL 服务器直接读取或写入文件。服务器必须可以访问该文件,并且必须从服务器的角度指定名称。当指定STDIN或STDOUT时,数据通过客户端和服务器之间的连接传输
这就是为什么对文件的 copy
命令仅限于 PostgreSQL 超级用户:文件必须存在于服务器上并且由服务器进程直接加载。
您应该改为使用:
cur.copy_from(r'C:\Users\n\Desktop\data.csv', temp_unicommerce_status)
如 this other answer 所建议,因为它在内部使用来自标准输入的 COPY
。
f = open(r'C:\Users\n\Desktop\data.csv', 'r')
cur.copy_from(f, temp_unicommerce_status, sep=',')
f.close()
文件必须作为对象传递。
由于您处理的是 csv 文件,因此有必要指定分隔符,因为默认为制表符
我解决这个问题的方法特别是使用 psychopg2 游标 class 函数 copy_expert(文档:http://initd.org/psycopg/docs/cursor.html)。 copy_expert 允许您使用 STDIN,因此无需为 postgres 用户授予超级用户权限。您对该文件的访问权限取决于客户端 (linux/windows/mac) 用户对该文件的访问权限
来自 Postgres COPY 文档 (https://www.postgresql.org/docs/current/static/sql-copy.html):
Do not confuse COPY with the psql instruction \copy. \copy invokes COPY FROM STDIN or COPY TO STDOUT, and then fetches/stores the data in a file accessible to the psql client. Thus, file accessibility and access rights depend on the client rather than the server when \copy is used.
您也可以严格保留访问 development_user 主文件夹和 App 文件夹的权限。
csv_file_name = '/home/user/some_file.csv'
sql = "COPY table_name FROM STDIN DELIMITER '|' CSV HEADER"
cursor.copy_expert(sql, open(csv_file_name, "r"))
您可以使用 d6tstack 使这变得简单
import d6tstack
import glob
c = d6tstack.combine_csv.CombinerCSV([r'C:\Users\n\Desktop\data.csv']) # single-file
c = d6tstack.combine_csv.CombinerCSV(glob.glob('*.csv')) # multi-file
c.to_psql_combine('postgresql+psycopg2://psqlusr:psqlpwdpsqlpwd@localhost/psqltest', 'tablename')
它还处理 data schema changes、create/append/replace table 并允许您使用 pandas.
预处理数据我知道这个问题已经得到解答,但这是我的两分钱。我正在添加更多描述:
您可以使用cursor.copy_from
方法:
首先,您必须创建一个 table 列数与您的 csv 文件相同的文件。
示例:
我的 csv 看起来像这样:
Name, age , college , id_no , country , state , phone_no
demo_name 22 , bdsu , 1456 , demo_co , demo_da , 9894321_
先创建一个table:
import psycopg2
from psycopg2 import Error
connection = psycopg2.connect(user = "demo_user",
password = "demo_pass",
host = "127.0.0.1",
port = "5432",
database = "postgres")
cursor = connection.cursor()
create_table_query = '''CREATE TABLE data_set
(Name TEXT NOT NULL ,
age TEXT NOT NULL ,
college TEXT NOT NULL ,
id_no TEXT NOT NULL ,
country TEXT NOT NULL ,
state TEXT NOT NULL ,
phone_no TEXT NOT NULL);'''
cursor.execute(create_table_query)
connection.commit()
现在您可以在需要三个参数的地方简单地使用 cursor.copy_from :
first file object , second table_name , third sep type
您现在可以复制了:
f = open(r'final_data.csv', 'r')
cursor.copy_from(f, 'data_set', sep=',')
f.close()
完成
我将 post 我 运行 在尝试将 csv 文件复制到基于 linux 的系统上的数据库时遇到的一些错误....
这是一个示例 csv 文件:
Name Age Height
bob 23 59
tom 56 67
您必须安装库 psycopg2(即 pip install psycopg2 或 sudo apt install python3-psycopg2)
您必须在系统上安装 postgres 才能使用 psycopg2 (sudo apt install postgresql-server postgresql-contrib )
现在您必须创建一个数据库来存储 csv,除非您已经使用 pre-existing 数据库
设置了 postgres
使用 Postgres 命令复制 CSV
安装 postgres 后,它会创建一个默认用户帐户,让您可以访问 postgres 命令
切换到postgres账号问题:sudo -u postgres psql
通过发出以下命令访问提示:psql
#创建数据库的命令 创建数据库mytestdb; #连接到数据库创建一个table \连接我的测试数据库; #create a table 具有相同的 csv 列名 创建 table 测试(姓名字符(50),年龄字符(50),身高字符(50)); #复制csv文件到table 用 csv header;
复制 mytestdb 'path/to/csv'
使用 PYTHON 复制 CSV 我 运行 将 CSV 文件复制到数据库的主要问题是我还没有创建数据库,但是这仍然可以通过 python 完成。
import psycopg2 #import the Postgres library
#connect to the database
conn = psycopg2.connect(host='localhost',
dbname='mytestdb',
user='postgres',
password='')
#create a cursor object
#cursor object is used to interact with the database
cur = conn.cursor()
#create table with same headers as csv file
cur.execute('''create table test(name char(50), age char(50), height char(50));''')
#open the csv file using python standard file I/O
#copy file into the table just created
f = open('file.csv','r')
cursor.copy_from(f, 'test', sep=',')
f.close()
#sample of code that worked for me
import psycopg2 #import the postgres library
#connect to the database
conn = psycopg2.connect(host='localhost',
dbname='database1',
user='postgres',
password='****',
port='****')
#create a cursor object
#cursor object is used to interact with the database
cur = conn.cursor()
#create table with same headers as csv file
cur.execute("CREATE TABLE IF NOT EXISTS test(**** text, **** float, **** float, ****
text)")
#open the csv file using python standard file I/O
#copy file into the table just created
with open('******.csv', 'r') as f:
next(f) # Skip the header row.
#f , <database name>, Comma-Seperated
cur.copy_from(f, '****', sep=',')
#Commit Changes
conn.commit()
#Close connection
conn.close()
f.close()