postgresql错误"在尝试导入csv文件时,在最后一个预期列之后的额外数据

时间:2018-02-26 15:25:07

标签: postgresql

我有一个包含8列的csv文件,但是我的表有6列,与csv文件的顺序相同。以下是csv和table

的格式和架构

example.csv

"CH_ID","CH_NAME","ADDRESS_1","ADDRESS_2","POST_CODE","MDSC","UPDATE_TIMESTAMP","DELETED_IND"
    403,"1182463","","10 St Pauls Court","SG2 8DN",1,2017-10-20 12:08:36,"N"

表格结构: -

CREATE TABLE ts_ch (
    id int8 NOT NULL DEFAULT nextval('ts_care_homes_seq'::regclass),
    ch_name varchar(200) NULL DEFAULT NULL::character varying,
    address_1 varchar(255) NULL DEFAULT NULL::character varying,
    address_2 varchar(255) NULL DEFAULT NULL::character varying,
    post_code varchar(10) NULL DEFAULT NULL::character varying,
    mdsc_patient int2 NULL,
    PRIMARY KEY (id)
)
WITH (
    OIDS=FALSE
) ;

我尝试了以下命令:

copy ts_care_homes(id, ch_name, address_1, address_2, post_code, mdsc_patient)  FROM '/var/tmp/care_home_v.csv' csv HEADER

但是我收到了这个错误:

  

最后一个预期列后的额外数据

1 个答案:

答案 0 :(得分:0)

您无法仅使用普通COPY命令加载部分CSV文件。

实现目标的一种方法是使用COPY ... FROM PROGRAM,例如这样(假设您已安装sed):

COPY laurenz.ts_ch(id, ch_name, address_1, address_2, post_code, mdsc_patient)
FROM PROGRAM
   'sed -e ''s/\(,\([^,]*\)\|\(\("[^"]*"\)*\)\)\{2,2\}$//'' /var/tmp/care_home_v.csv'
(FORMAT csv, HEADER on);

sed命令从CSV文件中删除最后两列。