Skip to main content
PHP Blog

Back to all posts

How to Update Jsonb Field In Postgresql?

Published on
6 min read
How to Update Jsonb Field In Postgresql? image

Best JSON Tools to Buy in October 2025

1 JSON at Work: Practical Data Integration for the Web

JSON at Work: Practical Data Integration for the Web

BUY & SAVE
$28.66 $49.99
Save 43%
JSON at Work: Practical Data Integration for the Web
2 Mastering Python and JSON: A Comprehensive Guide: From Basics to Advanced Techniques: Parsing, Manipulating, and Creating JSON Data with Python (Micro Learning | Python Book 4)

Mastering Python and JSON: A Comprehensive Guide: From Basics to Advanced Techniques: Parsing, Manipulating, and Creating JSON Data with Python (Micro Learning | Python Book 4)

BUY & SAVE
$3.90
Mastering Python and JSON: A Comprehensive Guide: From Basics to Advanced Techniques: Parsing, Manipulating, and Creating JSON Data with Python (Micro Learning | Python Book 4)
3 Azure Bicep QuickStart Pro: From JSON and ARM Templates to Advanced Deployment Techniques, CI/CD Integration, and Environment Management

Azure Bicep QuickStart Pro: From JSON and ARM Templates to Advanced Deployment Techniques, CI/CD Integration, and Environment Management

BUY & SAVE
$31.99
Azure Bicep QuickStart Pro: From JSON and ARM Templates to Advanced Deployment Techniques, CI/CD Integration, and Environment Management
4 JSON Quick Syntax Reference

JSON Quick Syntax Reference

BUY & SAVE
$29.99
JSON Quick Syntax Reference
5 DuckDB in Action

DuckDB in Action

BUY & SAVE
$44.00 $59.99
Save 27%
DuckDB in Action
6 Scalatra in Action

Scalatra in Action

BUY & SAVE
$42.68 $44.99
Save 5%
Scalatra in Action
7 Absolute Beginner's Guide to Javascript, Third Edition

Absolute Beginner's Guide to Javascript, Third Edition

BUY & SAVE
$31.03 $39.99
Save 22%
Absolute Beginner's Guide to Javascript, Third Edition
8 Pro Power BI Theme Creation: JSON Stylesheets for Automated Dashboard Formatting

Pro Power BI Theme Creation: JSON Stylesheets for Automated Dashboard Formatting

BUY & SAVE
$34.22 $59.99
Save 43%
Pro Power BI Theme Creation: JSON Stylesheets for Automated Dashboard Formatting
+
ONE MORE?

To update a JSONB field in PostgreSQL, you can use the jsonb_set() function. This function allows you to update specific keys or values within a JSONB field.

For example, if you have a table called users with a column details of JSONB data type, and you want to update the name key for a specific user with ID 1, you can use the following SQL query:

UPDATE users SET details = jsonb_set(details, '{name}', '"John Doe"'::jsonb) WHERE id = 1;

In this query, jsonb_set() function is used to update the name key in the details JSONB field for the user with ID 1. The jsonb_set() function takes three arguments: the JSONB field to update, the path to the key to update (specified as an array), and the new value to set.

You can also perform more complex updates, such as updating nested keys or arrays within a JSONB field, by specifying the path to the key to be updated.

Overall, using the jsonb_set() function in PostgreSQL allows you to efficiently update JSONB fields with specific data without having to replace the entire JSONB object.

How to update jsonb field based on condition in PostgreSQL?

To update a jsonb field in PostgreSQL based on a condition, you can use the UPDATE statement with a CASE statement to modify the JSON data based on your condition.

Here is an example of how to update a jsonb field named data in a table named my_table:

UPDATE my_table SET data = jsonb_set(data, '{key_to_update}', '"new_value"', true) WHERE condition = 'your_condition';

In this example:

  • jsonb_set function is used to update the data field by setting a new value for a specific key (key_to_update).
  • true as the fourth argument of jsonb_set function means the new value will overwrite the old value if the key already exists. If you want to merge or append values, use false instead of true.
  • WHERE clause is used to specify the condition under which the update should be applied.

You can customize this query further based on your specific requirements and the structure of your jsonb data.

How to update jsonb field in PostgreSQL using Node.js?

To update a jsonb field in PostgreSQL using Node.js, you can use the UPDATE query along with the SET clause to specify the new value for the jsonb field. Here is an example using the node-postgres package to connect to the PostgreSQL database and execute the update query:

const { Client } = require('pg');

const client = new Client({ user: 'your_username', host: 'localhost', database: 'your_database', password: 'your_password', port: 5432, });

client.connect();

// Define the update query const updateQuery = { text: 'UPDATE your_table SET your_jsonb_field = $1 WHERE id = $2', values: [ {key: 'new_value'}, 1 // Update record with id = 1 ], };

// Execute the update query client.query(updateQuery) .then(res => { console.log('Update successful'); }) .catch(err => { console.error('Error updating record:', err); }) .finally(() => { client.end(); });

In the code above, you would replace your_table, your_jsonb_field, and id with the actual table name, jsonb field to update, and the value to identify the record to update. You can specify the new jsonb value in the values array as shown.

Note that node-postgres is just one of the many PostgreSQL client libraries available for Node.js. Make sure to install the package by running npm install pg before using it in your project.

How to update specific key in jsonb field PostgreSQL?

You can update a specific key in a JSONB field in PostgreSQL by using the jsonb_set function. Here's an example of how you can update a specific key in a JSONB field:

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

In this example:

  • your_table is the name of your table
  • your_column is the name of the JSONB column you want to update
  • your_key is the key you want to update
  • "new_value" is the new value you want to set for the key
  • your_condition is the condition that determines which rows to update

Make sure to replace your_table, your_column, your_key, "new_value", and your_condition with your actual table, column, key, value, and condition.

How to update nested jsonb field in PostgreSQL?

To update a nested JSONB field in PostgreSQL, you can use the jsonb_set function. Below is an example of how to update a nested JSONB field in a table:

Let's say you have a table called users with a column called data that stores JSON data. The data column contains nested JSON data that you want to update.

Here is an example query to update a specific nested field in the data column:

UPDATE users SET data = jsonb_set(data, '{address, city}', '"New York"', TRUE) WHERE id = 1;

In this query:

  • jsonb_set(data, '{address, city}', '"New York"', TRUE) is used to update the city field in the address object to "New York".
  • TRUE in the jsonb_set function means that if the path doesn't exist, it should be created.

You can modify the path and value accordingly to update different nested fields in the JSONB column.

How to update jsonb field in PostgreSQL using Ruby on Rails?

To update a jsonb field in PostgreSQL using Ruby on Rails, you can use the update_column method provided by ActiveRecord.

Here's an example of how you can update a jsonb field in a Rails model:

# Assume you have a model named User with a jsonb field called data

user = User.find(1) updated_data = { key1: 'value1', key2: 'value2' }

Update the jsonb field with the new data

user.update_column(:data, updated_data)

Save the changes to the database

user.save

In this example, we're updating the data field of a User model with the updated_data hash. You can replace User with your own model name and data with the name of the jsonb field in your model.

Remember to replace User with your actual model name and data with the name of your jsonb field. Also, make sure to handle any error cases or validations that may be necessary for your specific use case.

What is the difference between updating json and jsonb field in PostgreSQL?

In PostgreSQL, JSON and JSONB are both data types that can store JSON data. The main difference between the two is how the data is stored and manipulated.

Updating a JSON field involves updating the entire JSON document, even if only a small part of it needs to be changed. This can be inefficient, especially for large JSON documents.

On the other hand, updating a JSONB field in PostgreSQL is much more efficient because it stores the data in a binary format that allows for more efficient storage and manipulation. With JSONB, only the specific parts of the JSON document that need to be updated are modified, rather than the entire document.

Overall, JSONB is preferred for storing and updating JSON data in PostgreSQL due to its efficiency and improved performance compared to the JSON data type.