Skip to main content
St Louis

Back to all posts

How to Update Jsonb String With Postgresql?

Published on
5 min read
How to Update Jsonb String With Postgresql? image

Best PostgreSQL Guide to Buy in October 2025

1 PostgreSQL: Up and Running: A Practical Guide to the Advanced Open Source Database

PostgreSQL: Up and Running: A Practical Guide to the Advanced Open Source Database

BUY & SAVE
$35.23 $44.99
Save 22%
PostgreSQL: Up and Running: A Practical Guide to the Advanced Open Source Database
2 Learn PostgreSQL: Use, manage, and build secure and scalable databases with PostgreSQL 16

Learn PostgreSQL: Use, manage, and build secure and scalable databases with PostgreSQL 16

BUY & SAVE
$44.99
Learn PostgreSQL: Use, manage, and build secure and scalable databases with PostgreSQL 16
3 PostgreSQL 16 Administration Cookbook: Solve real-world Database Administration challenges with 180+ practical recipes and best practices

PostgreSQL 16 Administration Cookbook: Solve real-world Database Administration challenges with 180+ practical recipes and best practices

BUY & SAVE
$34.91 $54.99
Save 37%
PostgreSQL 16 Administration Cookbook: Solve real-world Database Administration challenges with 180+ practical recipes and best practices
4 PostgreSQL DBA (v17, v16, v15, v14, v13) - 2025 2nd Edition: Full PostgreSQL Database Administrator's Guide, Secret DBA skills, High Availability, ... (GitHub link provided) (PostgreSQL 17)

PostgreSQL DBA (v17, v16, v15, v14, v13) - 2025 2nd Edition: Full PostgreSQL Database Administrator's Guide, Secret DBA skills, High Availability, ... (GitHub link provided) (PostgreSQL 17)

BUY & SAVE
$51.32
PostgreSQL DBA (v17, v16, v15, v14, v13) - 2025 2nd Edition: Full PostgreSQL Database Administrator's Guide, Secret DBA skills, High Availability, ... (GitHub link provided) (PostgreSQL 17)
5 PostgreSQL Query Optimization: The Ultimate Guide to Building Efficient Queries

PostgreSQL Query Optimization: The Ultimate Guide to Building Efficient Queries

BUY & SAVE
$43.58 $49.99
Save 13%
PostgreSQL Query Optimization: The Ultimate Guide to Building Efficient Queries
6 Mastering PostgreSQL 15: Advanced techniques to build and manage scalable, reliable, and fault-tolerant database applications

Mastering PostgreSQL 15: Advanced techniques to build and manage scalable, reliable, and fault-tolerant database applications

BUY & SAVE
$35.99 $61.99
Save 42%
Mastering PostgreSQL 15: Advanced techniques to build and manage scalable, reliable, and fault-tolerant database applications
7 Introduction to PostgreSQL for the data professional.

Introduction to PostgreSQL for the data professional.

BUY & SAVE
$24.99
Introduction to PostgreSQL for the data professional.
8 Beginning Databases with PostgreSQL: From Novice to Professional (Beginning From Novice to Professional)

Beginning Databases with PostgreSQL: From Novice to Professional (Beginning From Novice to Professional)

  • QUALITY ASSURANCE: RELIABLE GOOD CONDITION GUARANTEE FOR BUYERS.
  • ECO-FRIENDLY CHOICE: BUY USED BOOKS, SAVE MONEY, AND THE PLANET!
  • UNIQUE FINDS: DISCOVER RARE TITLES NOT AVAILABLE IN NEW EDITIONS!
BUY & SAVE
$41.94 $89.99
Save 53%
Beginning Databases with PostgreSQL: From Novice to Professional (Beginning From Novice to Professional)
9 Practical SQL, 2nd Edition: A Beginner's Guide to Storytelling with Data

Practical SQL, 2nd Edition: A Beginner's Guide to Storytelling with Data

BUY & SAVE
$25.48 $39.99
Save 36%
Practical SQL, 2nd Edition: A Beginner's Guide to Storytelling with Data
10 Mastering PostgreSQL 17: Elevate your database skills with advanced deployment, optimization, and security strategies

Mastering PostgreSQL 17: Elevate your database skills with advanced deployment, optimization, and security strategies

BUY & SAVE
$31.91
Mastering PostgreSQL 17: Elevate your database skills with advanced deployment, optimization, and security strategies
+
ONE MORE?

To update a JSONB string with PostgreSQL, you can use the jsonb_set function. This function allows you to replace or add values within a JSONB object.

Here's an example of how you can use jsonb_set to update a JSONB string in PostgreSQL:

UPDATE your_table SET your_column = jsonb_set(your_column, '{key}', '"new_value"', true) WHERE condition;

In this example:

  • your_table is the name of the table where the JSONB column is located.
  • your_column is the name of the JSONB column you want to update.
  • key is the key within the JSONB object that you want to update.
  • "new_value" is the new value that you want to set for the specified key.
  • true specifies whether to create the key if it doesn't exist.

You can also use jsonb_set to update nested values within a JSONB object by specifying the path to the nested key. This allows for more flexibility in updating complex JSONB structures.

What is the JSONB_POPULATE_RECORDSET function in PostgreSQL and how is it used for updating jsonb data?

The JSONB_POPULATE_RECORDSET function in PostgreSQL is used to update JSONB data in a table by populating record fields in a set of rows with corresponding JSONB values.

Here is an example of how the function can be used for updating JSONB data:

Suppose you have a table called "users" with a JSONB column called "attributes" that looks like this:

CREATE TABLE users ( id SERIAL PRIMARY KEY, name VARCHAR, attributes JSONB );

And you have data in the table that looks like this:

INSERT INTO users (name, attributes) VALUES ('Alice', '{"age": 30, "city": "New York"}'), ('Bob', '{"age": 25, "city": "Los Angeles"}');

Now, if you want to update the "age" field in the JSONB column for all rows in the "users" table, you can use the JSONB_POPULATE_RECORDSET function like this:

UPDATE users SET attributes = JSONB_POPULATE_RECORDSET( attributes, '{"age": age}' );

In this example, the JSONB_POPULATE_RECORDSET function is used to update the "age" field in the "attributes" column for all rows in the "users" table. The function takes two arguments: the current JSONB column value ("attributes") and a JSON object specifying the new values for the fields to be updated. In this case, we are updating the "age" field to a new value specified by the "age" column in the same table.

After running this update query, the "attributes" column for all rows in the "users" table will be updated with the new "age" value.

What is the JSONB_SET function in PostgreSQL and how is it used for updating jsonb data?

The JSONB_SET function in PostgreSQL is used to insert or update a value in a JSONB column. It takes three parameters: the original JSONB data, the path where the new value should be inserted or updated, and the new value to be inserted.

Here is the syntax for using JSONB_SET function:

JSONB_SET(jsonb, path, new_value)

  • jsonb: The original JSONB data on which the operation is to be performed.
  • path: The path where the new value should be inserted or updated.
  • new_value: The new value to be inserted or updated at the specified path.

For example, let's say we have a table named "person" with a JSONB column named "attributes" that contains the following data:

{ "name": "John Doe", "age": 30, "address": { "city": "New York", "zip": "10001" } }

We can use the JSONB_SET function to update the "age" attribute in the "attributes" column like this:

UPDATE person SET attributes = JSONB_SET(attributes, '{age}', '35') WHERE id = 1;

After running this query, the updated JSONB data would be:

{ "name": "John Doe", "age": 35, "address": { "city": "New York", "zip": "10001" } }

This is how we can use the JSONB_SET function in PostgreSQL to update JSONB data.

How to update jsonb string in PostgreSQL?

To update a JSONB string in PostgreSQL, you can use the UPDATE statement along with the SET clause. Here's an example of how you can update a JSONB string in a table:

UPDATE your_table_name SET your_jsonb_column_name = your_updated_jsonb_value WHERE your_condition;

In this query:

  • your_table_name is the name of the table containing the JSONB column.
  • your_jsonb_column_name is the name of the JSONB column you want to update.
  • your_updated_jsonb_value is the new JSONB value you want to set in the column.
  • your_condition is the condition that determines which rows to update. Make sure to specify this condition to avoid updating all rows in the table.

For example, if you have a table named employees with a JSONB column named details and you want to update the JSONB value for an employee with employee_id 123, you can use the following query:

UPDATE employees SET details = '{"name": "John Doe", "department": "IT"}' WHERE employee_id = 123;

This will update the JSONB value in the details column for the employee with employee_id 123 in the employees table.