MENU

PostgreSQL更新JSON数据

December 18, 2019 • 笔记阅读设置

pg更新JSON字段

jsonb_set()

示例:

  • user表的info字段为 jsonb 类型
  • info内city键更新为 Beijing  

    update \"user\"
    set \"info\" = jsonb_set(info, \'{city}\', \'\"Beijing\"\')
    where name = \'wang\';

若jsonb可能为空(null)

update \"user\"
set \"info\" = jsonb_set(coalesce(\"info\", \'{}\'), \'{city}\', \'\"Beijing\"\')
where name = \'wang\';