JIYIK CN >

Current Location:Home > Learning > DATABASE > PostgreSQL >

JSONB in PostgreSQL

Author:JIYIK Last Updated:2025/04/09 Views:

JSONB is a decomposed binary format for storing JSON data. If you follow Google Trends or do some research, you'll see that JSON is becoming increasingly popular in the development world.

Nowadays, NoSQL can store JSON data by default. It is becoming more and more popular because it is robust and we can nest objects inside another object.

Postgres allows users to create JSON and JSONB data types in SQL-type databases.

Creating a table in PostgreSQL using JSONB

Suppose we want to create a table for products. There can be various types of products.

For example, some of them may be in liters, some in grams, or some in pieces. Also, some types of data for a particular product may be different from others.

In this case, it is better to use JSON or JSONB to store this unfamiliar data. Given below is the example of creating a table with JSONB type data:

CREATE TABLE Products(
    id SERIAL PRIMARY KEY,
    seller TEXT NOT NULL,
    data JSONb NOT NULL
);

Inserting JSONB data type in PostgreSQL

Earlier, we created a table containing a JSONbcolumn with data of type . Now, let's populate the table.

As we all know, JSONB stores data in key-value format, the same as dictionary in Python. Let's fill the above table with some data.

INSERT INTO Products(seller,data)
VALUES
('Jones Heard',
'{ "name": "Milk Shake",
"price": "10$",
"ingredients":
    { "Milk":
        { "amount":"250ml","calorie":"100 Kcl"},
    "Coco Powder":
        { "amount":"50 gram", "calorie":"100 Kcl"}
    }
}'
);

Since we have successfully inserted the JSONB data into the table, it is time to view the added data. From the above JSON, we can notice that we have nested objects.

In the key data, we have name, priceand ingredient. In 成分, we have the ingredients and their quantities and calorie values.

So, it is a nested object. The motivation here is that we don't need complex joins to get such nested output.

SELECT id ,seller, jsonb_pretty(data) as Product_Details
FROM products;

Output:

 id |   seller    |         product_details
----+-------------+----------------------------------
  1 | Jones Heard | {                               +
    |             |     "name": "Milk Shake",       +
    |             |     "price": "10$",             +
    |             |     "ingredients": {            +
    |             |         "Milk": {               +
    |             |             "amount": "250ml",  +
    |             |             "calorie": "100 Kcl"+
    |             |         },                      +
    |             |         "Coco Powder": {        +
    |             |             "amount": "50 gram",+
    |             |             "calorie": "100 Kcl"+
    |             |         }                       +
    |             |     }                           +
    |             | }
(1 row)

In the command, we have used jsonb_pretty()the function. It formats the JSON object with necessary indentation.

Otherwise the entire JSON appears on one line, making it difficult to understand the relationships between objects.

Pros and Cons of JSONB in ​​PostgreSQL

Although there is a JSON type in Postgres, they have also introduced the JSONB format. As we can see, declarations and other queries are very similar to JSON.

Here are some benefits of using JSONB over JSON:

  1. More efficient than JSON.
  2. It allows for faster processing since it is a decomposed binary file.
  3. Supports indexing, JSON does not.

Besides, it also has some disadvantages. Here are some disadvantages:

  1. Slower input.
  2. It takes up more disk space than JSON because it leaves a larger footprint.
  3. In some cases, aggregate functions can slow down the process.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Terminate the PostgreSQL connection

Publish Date:2025/04/11 Views:199 Category:PostgreSQL

In this article, we will learn how to terminate a PostgreSQL session. Any open connections are run by background processes or tasks, PSQL which may no longer exist despite exiting the user interface or command line tool. Use ps -ef or grep

Single query to rename and change column type in PostgreSQL

Publish Date:2025/04/11 Views:166 Category:PostgreSQL

This article describes how to rename a column and change its type in PostgreSQL using only a single query. Renaming and changing column types in MySQL In MySQL , if you want to change the column type and rename it, you can use a simple stat

Joining columns using Select in PostgreSQL

Publish Date:2025/04/11 Views:176 Category:PostgreSQL

MySQL PostgreSQL is an object-relational database system, which means it can support more complex data types than its competitors . Today we will learn how to use SELECT the operator to join the columns of a table. Using operators to || joi

Using CASE in PostgreSQL

Publish Date:2025/04/11 Views:124 Category:PostgreSQL

This article shows how to use the statement in PostgreSQL CASE . CASE How to use the statement in PostgreSQL case Statements are similar to those in general-purpose programming languages if-else . But in SQL, if you want to write IF-ELSE ,

Using NOT IN with subqueries in PostgreSQL

Publish Date:2025/04/11 Views:93 Category:PostgreSQL

NOT IN The inverts the result of NOT simply using IN the operator. NOT IN The right side of the operator must have a subquery in which multiple columns are returned to check whether the expression matches the data. NOT IN Tends to return tr

Using variables in PostgreSQL

Publish Date:2025/04/11 Views:171 Category:PostgreSQL

This article will demonstrate how we can declare and assign values ​​to variables in PostgreSQL. In PostgreSQL, DECLARE variables are declared using Often you will need variables in your PL/SQL scripts. In DECLARE the section called , y

Connect to PostgreSQL using a password

Publish Date:2025/04/11 Views:171 Category:PostgreSQL

This article shows various ways to connect to PostgreSQL using a password. It can be through the command line, pgpass a file, PGPASSWORD an environment variable or a connection string. Connecting to PostgreSQL with a password using the comm

Deleting a database in PostgreSQL via PSQL

Publish Date:2025/04/11 Views:166 Category:PostgreSQL

There are two ways to access PostgreSQL objects and databases on your system. One is through an interface, such as a graphical interface like PGADMIN, and the other is the basic command line tool psql. Today, we will look at DROP DATABASE t

Using the database in PostgreSQL

Publish Date:2025/04/11 Views:132 Category:PostgreSQL

This article demonstrates connecting to a database, creating a new database, and creating a table in PostgreSQL. Available databases in PostgreSQL You can run the following command after opening the Postgres command line to view all availab

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial