How to update jsonb string with postgresql?

Member

by ryleigh , in category: MySQL , 5 months ago

How to update jsonb string with postgresql?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by filiberto , 5 months ago

@ryleigh 

To update a JSONB column in PostgreSQL, you can use the jsonb_set function. Here's an example of how to update a JSONB column called data in a table called my_table:

1
2
3
UPDATE my_table
SET data = jsonb_set(data, '{key1}', '"new_value"', true)
WHERE condition;


In this example:

  • data is the name of the JSONB column you want to update
  • {key1} is the path to the key you want to update within the JSONB column
  • "new_value" is the new value you want to set
  • true indicates that the function should create any missing keys along the path


Make sure to replace my_table with the actual name of your table, and adjust the WHERE condition as needed to specify which rows you want to update.

Related Threads:

How to extract text from jsonb array in postgresql?
How to update jsonb column using laravel query?
How to order by on a key inside jsonb column in postgresql?
How to query nested jsonb format data column in postgresql?
How to push to an array inside of a json object in a jsonb column in postgresql?
How to update partial value in postgresql?