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 do I use PostgreSQL ZonedDateTime to store date and time information?
- How can I use PostgreSQL XOR to compare two values?
- How can I troubleshoot zero damaged pages in PostgreSQL?
- How do I use PostgreSQL's XMLTABLE to parse XML data?
- How do I parse XML data using PostgreSQL?
- How do I access the PostgreSQL wiki?
- How do I show tables in PostgreSQL?
- How can I set a PostgreSQL interval to zero?
- How can Zalando use PostgreSQL to improve its software development?
- How can I use PostgreSQL with YAML?
See more codes...