psql copy命令将无法正确解释NULL值

时间:2018-09-29 17:07:18

标签: postgresql psql

我正在尝试将csv(带有标头)中的值复制到表中。我看过this answer,它指出要指定NULL值,但它似乎对我没有影响。这就是我所拥有的:

CREATE TABLE stops
(
  stop_id           text PRIMARY KEY,
  --stop_code         text NULL,
  stop_name         text NOT NULL,
  --stop_desc         text NULL,
  stop_lat          double precision NOT NULL,
  stop_lon          double precision NOT NULL,
  zone_id           integer NULL,
  stop_url          text NULL,
  location_type     boolean NULL,
  parent_station    text NULL
);

\copy stops from './stops.txt' with csv header NULL AS ''

我也尝试使用\N字符,如下所示:

\copy stops from './stops.txt' with csv header NULL AS '\N'

但这似乎没有效果。

我还尝试过尝试找到here的解决方案,它看起来像这样:

\copy agency from './agency.txt' WITH (FORMAT csv header, FORCE_NULL(zone_id))

但这似乎在csv_header部分引发了语法错误。

版本为9.6。

这是csv的摘录:

stop_id,stop_name,stop_lat,stop_lon,zone_id,stop_url,location_type,parent_station
"de:07334:1714:1:1","Wörth Alte Bahnmeisterei","49.048742345982","8.26622538039577","","","","Parent1714"
"de:07334:1714:1:2","Wörth Alte Bahnmeisterei","49.0484420719247","8.26673742010779","","","","Parent1714"
"de:07334:1721:1:1","Maximiliansau Eisenbahnstraße","49.0373071007148","8.29789997731824","","","","Parent1721"
"de:07334:1721:2:2","Maximiliansau Eisenbahnstraße","49.0371363175998","8.29896897250649","","","","Parent1721"

1 个答案:

答案 0 :(得分:1)

  

但这似乎在csv_header部分抛出了语法错误。

在csv之后加上逗号:

\copy agency from './agency.txt' WITH (FORMAT csv, HEADER, FORCE_NULL(zone_id,location_type))

使用空字符串指定NULL时,非文本列显然需要FORCE_NULL