create table target (
id serial,
val text,
primary key (id),
unique (val)
);
-- Вставляем Раз и Два
insert into target (val) values ('One'), ('Two');
create table source (
id serial,
val text,
primary key (id),
unique (val)
);
-- Вставляем Раз и Два
insert into source (val) values ('One'), ('Three');
-- Ошибочка
insert into target (val) select val from source;
-- Попробуем так
insert into target (val) select val from source on conflict (val) do nothing;
select * from target;
create table target2 (
id serial,
val text,
primary key (id),
unique (val)
);
-- Вставляем Раз и Два
insert into target2 (val) values ('One'), ('Two');
insert into target2 (val)
select val from source
where not exists (select true from target2 where target2.val = source.val);
select * from target2;
create table target3 (
id serial,
val text,
primary key (id),
unique (val)
);
-- Вставляем Раз и Два
insert into target3 (val) values ('One'), ('Two');
merge into target3 t
using source s on t.val = s.val
when not matched then insert (val) values (val);
select * from target3;