如何使用复制命令将 csv 文件复制到 postgresql?

How to copy a csv file to postgresql using the copy command?

我编写了以下脚本来将 csv 文件上传到 postgresql 数据库。

import psycopg2
import keys
con = psycopg2.connect(
            host = keys.keys['host'],
            database = keys.keys['database'],
            user = keys.keys['user'],
            password = keys.keys['password'])

#cursor 
cur = con.cursor()

#execute query
#Already created ___#cur.execute("CREATE TABLE accounts (user_id serial PRIMARY KEY, username VARCHAR ( 50 ) UNIQUE NOT NULL, password VARCHAR ( 50 ) NOT NULL, email VARCHAR ( 255 ) UNIQUE NOT NULL, created_on TIMESTAMP NOT NULL, last_login TIMESTAMP)")

cur.execute("""\COPY "MyData" FROM 'C:\FILES\TestData.csv' DELIMITER ',' CSV HEADER;""")

#commit the transcation 
con.commit()

#close the cursor
cur.close()

#close the connection
con.close()

但它返回了以下错误:-

SyntaxError: syntax error at or near "\"
LINE 1:  \COPY "MyData" FROM 'C:\FILES\TestData.csv' DELIMITER ',' C...

我不是root用户,所以无法直接使用COPY命令

嗯。

您可以使用 psycopg2 的 copy_from -> https://www.psycopg.org/docs/cursor.html#cursor.copy_from

所以你的代码应该是这样的:

import psycopg2
import keys
con = psycopg2.connect(
            host = keys.keys['host'],
            database = keys.keys['database'],
            user = keys.keys['user'],
            password = keys.keys['password'])

#cursor 
cur = con.cursor()

#execute query
#Already created ___#cur.execute("CREATE TABLE accounts (user_id serial PRIMARY KEY, username VARCHAR ( 50 ) UNIQUE NOT NULL, password VARCHAR ( 50 ) NOT NULL, email VARCHAR ( 255 ) UNIQUE NOT NULL, created_on TIMESTAMP NOT NULL, last_login TIMESTAMP)")


with open('C:\Files\TestData.csv', 'r') as acc:
    next(acc) # This will skip the header
    cur.copy_from(acc, 'accounts', sep=',')

#commit the transcation 
con.commit()

#close the cursor
cur.close()

#close the connection
con.close()


希望这能回答您的问题。