简体   繁体   中英

Importing CSV file into a psql table

I have a csv file with 2 columns, one column is an id and the other one a name. No csv header.

"5138334","Here's Where the Story Ends"
"36615796","W31CZ-D"
"10283436","Elliant"
"8773661","Dobos torte"
"33139146","RTP Informação"
"2846867","The Legend of the Rollerblade Seven"
"36001757","Prescription Monitoring Program"
"2574520","Greg Wells"
"4498288","Catonsville Community College"
"15429275","Nozières"
"31736463","Bályok"

how do I insert this into a psql table?

I have tried creating a table.

create table csvtable(id bigserial not null primary key, 
                      csv_id int not null, 
                      csv_title varchar(100) not null);

and other variations without the id column(I tried making my own id in case the existing id wasn't unique)

and I have tried inserting the data through the copy command.

copy csvtable from 'file.csv' with csv;

and other variations with delimeter, etc. but no luck.

You need to specify which columns you are copying:

\copy csvtable(csv_id, csv_title) FROM 'data.csv' WITH (FORMAT csv)

Note that this is using \\copy from psql rather than COPY (which might not be needed if the file is on the same server).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM