简体   繁体   English

将CSV文件导入到psql表中

[英]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. 我有2列的csv文件,一列是id,另一列是名称。 No csv header. 没有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"

how do I insert this into a psql table? 如何将其插入到psql表中?

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) 以及其他没有ID列的变体(我尝试制作自己的ID,以防现有ID不唯一)

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). 请注意,这是使用来自psql的\\copy而不是COPY (如果文件位于同一服务器上,则可能不需要)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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