postgresqlHow can I use jsonb_set to update a PostgreSQL database?
JSONB_SET is a PostgreSQL function used to update fields in a JSONB column. It allows for the insertion of new values, as well as the replacement of existing values.
Example
UPDATE my_table
SET my_jsonb_column = jsonb_set(my_jsonb_column, '{name}', '"John"')
WHERE id = 1;
The above example will update the my_jsonb_column
in my_table
with the id
of 1
and set the name
field in the JSONB column to John
.
Code explanation
UPDATE my_table
- This is used to indicate which table we are updating.SET my_jsonb_column = jsonb_set(my_jsonb_column, '{name}', '"John"')
- This is used to set themy_jsonb_column
to the return value ofjsonb_set
with thename
field and the valueJohn
.WHERE id = 1
- This is used to indicate which record we are updating.
Helpful links
More of Postgresql
- How can I use PostgreSQL's "zero if null" feature?
- How can I use PostgreSQL and ZFS snapshots together?
- How can I troubleshoot zero damaged pages in PostgreSQL?
- How can I set a PostgreSQL interval to zero?
- How can I get a value from a PostgreSQL XML column?
- How do I use PostgreSQL regex to search for a specific pattern?
- How do I use PostgreSQL's ON CONFLICT DO UPDATE statement?
- How do I grant all privileges on a PostgreSQL schema?
- How can Zalando use PostgreSQL to improve its software development?
- How do I use PostgreSQL and ZFS together?
See more codes...