我有一个CSV文件my_table.csv
,如下所示:
"dt_start","my_int_value","my_double_value","dt_version"
"2022-01-02 00:00:00",2,2.2,"2022-01-02 00:00:00"
"2022-01-03 00:00:00",3,3.3,"2022-01-03 00:00:00"
现在,我只想使用psycopg3
包中的instructions(使用psycopg==3.1.3
和psycopg-binary==3.1.3
)将此文件从Python导入到PostgreSQL数据库的表my_table
中。
我的代码如下所示:
import os
import psycopg
table_name = "my_table"
conn = psycopg.connect(
dbname="MY_DB",
user="MY_USER",
password="MY_PW",
host="MY_HOST",
port="MY_PORT",
)
with conn:
with conn.cursor() as cur:
# create table
cur.execute(
f"""
CREATE TABLE IF NOT EXISTS {table_name} (
dt_start TIMESTAMP NOT NULL,
my_int_value INT NOT NULL,
my_double_value DOUBLE PRECISION NOT NULL,
dt_version TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(dt_start, my_int_value, my_double_value, dt_version)
)
"""
)
# clear table
cur.execute(f"TRUNCATE {table_name}")
conn.commit()
# insert one row
cur.execute(
f"""INSERT INTO {table_name}"""
+ f""" (dt_start, my_int_value, my_double_value, dt_version)"""
+ f""" VALUES (%s, %s, %s, %s)""",
("2022-01-01 00:00:00", 1, 1.1, "2022-01-01 00:00:00"),
)
conn.commit()
# fetch it
cur.execute(f"""SELECT * FROM {table_name}""")
print(cur.fetchall())
# this breaks with "psycopg.errors.InvalidDatetimeFormat"
with open(f"""{table_name}.csv""", "r") as f:
with cur.copy(f"COPY {table_name} FROM STDIN") as copy:
while data := f.read(100):
copy.write(data)
conn.commit()
一些示例数据的第一个步骤可以正常工作,但CSV导入会因错误而中断,例如:
psycopg.errors.InvalidDatetimeFormat: invalid syntax for type timestamp without time zone: »"dt_start","my_int_value","my_double_value","dt_version"«
CONTEXT: COPY my_table, Row 1, Column dt_start: »"dt_start","my_int_value","my_double_value","dt_version"«
同时,我还尝试了不同的导入变体和不同的日期时间格式,但都导致了相同的错误。
有关于如何解决此问题的提示吗?
2条答案
按热度按时间bhmjp9jg1#
j8ag8udp2#
该文件包含一个头文件,请告诉Postgres:
检查the documentation.中的语法