【问题标题】:Copying CSV into Postgres table - invalid input syntax for type timestamp with time zone: "1"将 CSV 复制到 Postgres 表中 - 带有时区的时间戳类型的输入语法无效:“1”
【发布时间】: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


【解决方案1】:

感谢@richyen 的提示。我以前一直在使用具有不同格式日期数据的 .csv 文件。我正在将一个新的 .csv 文件复制到 Postgres 服务器,但结果证明存在缓存问题,这意味着新文件实际上并未被复制。通过使用head filename.csv 检查 csv 文件,我能够看到这一点。终止我正在使用的 linux shell 并重新开始新会话使我能够成功复制新文件。

【讨论】:

    猜你喜欢
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-11
    相关资源
    最近更新 更多