PgSQL upsert批量查询插入或更新(insert select/on conflict do update踩坑记录)

insert into t --进行插入
values(1,'name') 
ON CONFLICT(id) --如果id这个键存在
do update set --更新以下字段
name=EXCLUDED.name ;
 
 
insert into t (a1,b1,c1)
select a2,b2,c2
from t2 
on conflict(a1) 
do update set 
(b1,c1) = (1,2)

conflict里的字段必须为主键或者唯一索引,可以多个字段作为唯一索引,在数据库设置唯一,不然会报

原文地址:https://www.cnblogs.com/h-c-g/p/15237405.html