Sql 将大量行导入具有重复密钥冲突的Postgres

Sql 将大量行导入具有重复密钥冲突的Postgres,sql,postgresql,import,key,postgresql-copy,Sql,Postgresql,Import,Key,Postgresql Copy,在Postgres 9.6中,是否可以使用COPY命令在重复键上获得上传功能?我有一个正在导入Postgres的CSV文件,但它可能包含一些重复的密钥冲突,因此COPY命令会给出一个错误,并在遇到这些错误时终止 该文件非常大,因此可能无法在应用程序代码中对其进行预处理(以便处理可能导致重复密钥冲突的行),因为所有密钥可能都不适合内存 将可能包含重复密钥冲突的大量行导入Postgre的最佳方法是什么?示例: t=# create table s90(i int primary key, t tex

在Postgres 9.6中,是否可以使用
COPY
命令在重复键上获得
上传功能?我有一个正在导入Postgres的CSV文件,但它可能包含一些重复的密钥冲突,因此
COPY
命令会给出一个错误,并在遇到这些错误时终止

该文件非常大,因此可能无法在应用程序代码中对其进行预处理(以便处理可能导致重复密钥冲突的行),因为所有密钥可能都不适合内存

将可能包含重复密钥冲突的大量行导入Postgre的最佳方法是什么?

示例:

t=# create table s90(i int primary key, t text);
CREATE TABLE
t=# insert into s90 select 1,'a';
INSERT 0 1
t=# copy s90 from stdin delimiter ',';
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself.
>> 1,'b'
>> 2,'c'
>> \.
ERROR:  duplicate key value violates unique constraint "s90_pkey"
DETAIL:  Key (i)=(1) already exists.
CONTEXT:  COPY s90, line 1
复制的解决方法:

t=# create table s91 as select * from s90 where false;;
SELECT 0
t=# copy s91 from stdin delimiter ',';
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself.
>> 1,'b'
>> 2,'c'
>> \.
COPY 2
t=# with p as (select s91.* from s91 left outer join s90 on s90.i=s91.i where s90.i is null)
insert into s90 select * from p;
INSERT 0 1
t=# select * from s90;
 i |  t
---+-----
 1 | a
 2 | 'c'
(2 rows)

副本尚未插入。您可以复制到其他表,然后插入differece@VaoTsun你能用一个例子来说明你的意思吗?我不知道如何实现“插入差异”部分,我确实做到了。在您的例子中,您有UK,因此您必须插入from
left outer join…其中original.UK_att为null
。如果要插入完全不同的行(所有列),请以类似方式使用