【发布时间】:2020-03-30 23:49:42
【问题描述】:
我正在 Postgres 中创建一个新表,我想将数据从 CSV 文件复制到该表中。 CSV数据是这样的:
nodeid,usertoken,application,starttime,endtime,count1,count2,count3,count4,count5,utctimestamp,timestamp
ABCDE,84a6f486-97f1-4449-ad63-ae36e51392bd,AB,2019-09-15 00:09:10.365+00,2019-09-15 00:11:57.403+00,1,0,0,1,2,2019-09-15 00:11:57.403+00,2019-09-15 00:11:57.403+00
我这样创建 Postgres 表:
CREATE TABLE test_table
(
nodeid text,
usertoken text,
application text,
starttime timestamp with time zone,
endtime timestamp with time zone,
count1 integer,
count2 integer,
count3 integer,
count4 integer,
count5 integer,
utctimestamp timestamp with time zone,
"timestamp" timestamp with time zone,
msgid serial NOT NULL,
CONSTRAINT test_table_pkey PRIMARY KEY (msgid)
)
WITH (
OIDS=FALSE
);
然后我通过psql 使用此命令,尝试从 CSV 文件中复制数据(其中 myfilename 是 csv 文件的名称)
COPY test_table(nodeid, usertoken, application, starttime, endtime, count1, count2, count3, count4, count5, utctimestamp, timestamp) FROM 'myfilename.csv' DELIMITER ',' CSV HEADER;
我得到错误:
ERROR: invalid input syntax for type timestamp with time zone: "1"
CONTEXT: COPY test_table, line 2, column endtime: "1"
错误似乎表明count1 值被用作endtime 的插入值,但是我不明白为什么会这样
【问题讨论】:
-
这是哪个版本的 Postgres?我尝试使用 v.11.5,它工作正常。一种可能性是您可能有两个
myfilename.csv副本,而您正在阅读错误的副本 - 也许尝试使用您认为正在使用的那个的绝对路径?
标签: sql postgresql csv psql