将 CSV 文件导入 psql table
Importing CSV file into a psql table
我有一个包含 2 列的 csv 文件,一列是 ID,另一列是名称。没有 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"
如何将其插入到 psql table?
我试过创建 table。
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;
和其他带有分隔符等的变体,但没有运气。
您需要指定要复制的列:
\copy csvtable(csv_id, csv_title) FROM 'data.csv' WITH (FORMAT csv)
请注意,这是使用来自 psql 的 \copy
而不是 COPY
(如果文件在同一台服务器上,则可能不需要)。
我有一个包含 2 列的 csv 文件,一列是 ID,另一列是名称。没有 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"
如何将其插入到 psql table?
我试过创建 table。
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;
和其他带有分隔符等的变体,但没有运气。
您需要指定要复制的列:
\copy csvtable(csv_id, csv_title) FROM 'data.csv' WITH (FORMAT csv)
请注意,这是使用来自 psql 的 \copy
而不是 COPY
(如果文件在同一台服务器上,则可能不需要)。