1

我想将 csv 文件导入 psql db ...

我已经搜索并尝试使用副本,也提到了https://www.commandprompt.com/ppbook/r23528,我使用的语法是:

copy location from 'file.csv' with delimiters '\t' NULL as 'NULL' CSV;

注意:我的 csv 文件由 'tab' 分隔。

第一次执行上述 cmd 后,显示:

ERROR:  syntax error at or near ""\t""
LINE 1: ...om 'file.csv' using delimiters "\t" ;

第二次,

ERROR:  syntax error at or near "copy"
LINE 2: copy location from 'file.csv' using d... 

编辑后,

我第二次尝试使用 like ,

copy location from 'file.csv' with delimiter E'\t' NULL as 'NULL' CSV;

ERROR: extra data after last expected column 
CONTEXT: COPY location, line 1: "AD AD100   Canillo  42.5833    1.6667  6" 

我的表描述是

Table "public.location"

 Column    |          Type          | Modifiers 
-------------+------------------------+-----------
 countrycode | character varying(2)   | 
 postalcode  | character varying(20)  | 
 place       | character varying(100) | 
 state       | character varying(20)  | 
 country     | character varying(100) | 
 country2    | character varying(20)  | 
 community   | character varying(100) | 
 community2  | character varying(20)  | 
 latitude    | double precision       | 
 longitude   | double precision       | 
 accuracy    | integer                | 

如何解决这个问题?

输入:

AD      AD100   Canillo                                                 42.5833 1.6667  6
AD      AD200   Encamp                                                  42.5333 1.6333  6
AD      AD300   Ordino                                                  42.6    1.55    6
AD      AD400   La Massana                                                      42.5667 1.4833  6
AD      AD500   Andorra la Vella                                                        42.5    1.5     6
AD      AD600   Sant Julià de Lòria                                                     42.4667 1.5     6
AD      AD700   Escaldes-Engordany                                                      42.5    1.5667  6
AR      3636    "POZO CERCADO (EL CHORRO (F), DPTO. RIVADAVIA (S))"     SALTA   A                                       -23.4933        -61.9267        3
AR      4123    LAS SALADAS     SALTA   A                                       -25.7833        -64.5   4
AR      4126    LA MARAVILLA    SALTA   A                                       -26.0833        -65.263 3
AR      4126    TALA    SALTA   A                                       -26.1167        -65.2833        4
AR      4126    LA ASUNCION     SALTA   A                                       -26.0833        -65.263 3
AR      4126    BRETE   SALTA   A                                       -26.0667        -65.3667        4
AR      4126    EL SUNCHAL      SALTA   A                                       -26.0833        -65.263 3
AR      4126    CEIBAL  SALTA   A                                       -26.1   -65.0167        4
AR      4126    BARADERO        SALTA   A                                       -26.0833        -65.263 3
AR      4126    CANDELARIA      SALTA   A                                       -26.1   -65.1   4
AR      4126    ALEM    SALTA   A                                       -26.0833        -65.263 3
AR      4126    EL BRETE        SALTA   A                                       -26.0667        -65.3667        4
AR      4126    EL CUIBAL       SALTA   A                                       -26.0833        -65.263 3
AR      4126    EL JARDIN       SALTA   A                                       -26.0833        -65.3833        4
AR      4126    OVEJERO SALTA   A                                       -26.0833        -65.263 3
AR      4126    LOS MOGOTES     SALTA   A                                       -26.0333        -65.2   4
AR      4126    "MIRAFLORES (TALA, DPTO. CANDELARIA)"   SALTA   A                                       -26.0833        -65.263 3

这是一些例如输入...

当我在 ms xcel 工作表中打开此文件时,使用分隔符选项卡将数据正确分隔到相关列。

输入带有制表符和空值的文件,

AD\tAD100\tCanillo\t\n\t\n\t\n\t\n\t\n\t\n\t42.5833\t1.6667\t6
AD\tAD200\tEncamp\t\n\t\n\t\n\t\n\t\n\t\n\t42.5333\t1.6333\t6
AR\t3636\t"POZO CERCADO (EL CHORRO (F), DPTO. RIVADAVIA (S))"\tSALTA\tA\t\n\t\n\t\n\t\n\t-23.4933\t-61.9267\t3
4

1 回答 1

6

根据声明中的文档COPY,它应该是WITH DELIMITER而不是DELIMITERS

此外,COPY语法已更改,鼓励您使用以下内容:

COPY location FROM 'file.csv' WITH (FORMAT csv, DELIMITER '\t',  NULL 'NULL');

编辑:

我已经使用提供的信息来测试如何COPY处理呈现的数据:

CREATE TABLE atest (
    countrycode text CHECK (length(countrycode) <= 2),
    postalcode  text CHECK (length(postalcode) <=20), 
    place       text CHECK (length(place) <= 100), 
    state       text CHECK (length(state) <= 20), 
    country     text CHECK (length(country) <= 100), 
    country2    text CHECK (length(country2) <= 20), 
    community   text CHECK (length(community) <= 100), 
    community2  text CHECK (length(community2) <= 20),
    latitude    double precision,
    longitude   double precision,
    accuracy    integer);

我已经使用了上面提到的命令(采用了一点):

COPY atest FROM '/Users/viy/atest2.csv'
     WITH (FORMAT csv, DELIMITER E'\t',  NULL '\n'); 

并得到与上述相同的错误。这是意料之中的,因为空字段太多。让我们看看CSV数据以及它如何匹配表定义(全部\t替换为新行):

AD       countrycode
AD100    postalcode
Canillo  place
\n       state
\n       country
\n       country2
\n       community
\n       community2
\n       latitude
42.5833  longitude
1.6667   accuracy
6        <-- this one gives the error!

因此,为了加载数据,您必须检查源数据并在中间删除一个额外的空白字段。另一种方法是调整表的定义以匹配您的源数据。

于 2012-07-06T13:38:21.367 回答