How to copy from CSV file to PostgreSQL table with headers in CSV file?

Stanley Cup Phil picture Stanley Cup Phil · Jul 15, 2013 · Viewed 155.5k times · Source

I want to copy a CSV file to a Postgres table. There are about 100 columns in this table, so I do not want to rewrite them if I don't have to.

I am using the \copy table from 'table.csv' delimiter ',' csv; command but without a table created I get ERROR: relation "table" does not exist. If I add a blank table I get no error, but nothing happens. I tried this command two or three times and there was no output or messages, but the table was not updated when I checked it through PGAdmin.

Is there a way to import a table with headers included like I am trying to do?

Answer

G. Cito picture G. Cito · Jul 16, 2013

This worked. The first row had column names in it.

COPY wheat FROM 'wheat_crop_data.csv' DELIMITER ';' CSV HEADER