How to update jsonb string with postgresql?

Member

by ryleigh , in category: MySQL , a month ago

How to update jsonb string with postgresql?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by filiberto , a month 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.