0

How do I do the spread syntax in postgres UPDATE

UPDATE TABLE <table_name> set data={...original, ...<new_data>} where id='<id>'

in postgres

Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
Jackstine
  • 486
  • 4
  • 12

1 Answers1

0

where {"a": "b"} is the JSON object spreading into the current data

update <table_name> set data = data || '{"a":"b"}' where id='<id>'

when you need to change a nested value use jsonb_set

update <table_name> set data = jsonb_set(data,'{"a","b"}', '{"attr": "value"}', true) where id='<id>'
Jackstine
  • 486
  • 4
  • 12