B B
B B

Reputation: 71

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.

Upvotes: 0

Views: 193

Answers (1)

fphilipe
fphilipe

Reputation: 10054

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).

Upvotes: 2

Related Questions