将CSV文件导入到psql表中

时间:2019-04-16 16:10:16

标签: sql psql

我有一个包含2列的csv文件,一列是ID,另一列是名称。没有csv标头。

"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"

如何将其插入到psql表中?

我尝试创建表。

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

和其他没有id列的变体(我尝试制作自己的ID,以防现有ID不唯一)

并且我试图通过复制命令插入数据。

copy csvtable from 'file.csv' with csv;

和其他带有定界符的变化等,但是没有运气。

1 个答案:

答案 0 :(得分:2)

您需要指定要复制的列:

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

请注意,这是从psql使用\copy,而不是COPY(如果文件位于同一服务器上,则可能不需要)。

相关问题