Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
388 views
in Technique[技术] by (71.8m points)

sql - pgAdmin4: Importing a CSV

I am trying to import a CSV using pgAdmin4. I created the table using the query,

CREATE TABLE i210_2017_02_18
(
  PROBE_ID character varying(255),
  SAMPLE_DATE timestamp without time zone,
  LAT numeric,
  LON numeric,
  HEADING integer,
  SPEED integer,
  PROBE_DATA_PROVIDER character varying(255),
  SYSTEM_DATE timestamp without time zone
)

The header and first line of my CSV read is...

PROBE_ID,SAMPLE_DATE,LAT,LON,HEADING,SPEED,PROBE_DATA_PROVIDER,SYSTEM_DATE
841625st,2017-02-18 00:58:19,34.11968,-117.80855,91.0,9.0,FLEET53,2017-02-18 00:58:58

When I try to use the import dialogue, the process fails with Error Code 1:

ERROR:  invalid input syntax for type timestamp: "SAMPLE_DATE"
CONTEXT:  COPY i210_2017_02_18, line 1, column sample_date: "SAMPLE_DATE"

Nothing seems wrong to me - any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

According to your table structure, this import will fail in the columns HEADING and SPEED, since their values have decimals and you declared them as INTEGER. Either remove the decimals or change the column type to e.g. NUMERIC.

Having said that, just try this from pgAdmin (considering that file and database are in the same server):

COPY i210_2017_02_18 FROM '/home/jones/file.csv' CSV HEADER;

In case you're dealing with a remote server, try this using psql from your console:

$ cat file.csv | psql yourdb -c "COPY i210_2017_02_18 FROM STDIN CSV HEADER;"

You can also check this answer.

In case you really want to stick to the pgAdmin import tool, which I discourage, just select the Header option and the proper Delimiter:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...