Introduction to PostgreSQL jsonb Data Type
PostgreSQL is an open-source relational database management system that supports various data types, including the jsonb data type. jsonb is a lightweight data interchange format known for its readability and simplicity, which makes it very popular. In PostgreSQL, the jsonb data type can be used to store and query unstructured data.
In PostgreSQL, both json and jsonb are data types used for storing JSON data, but they have some differences:
- Storage format: The
jsontype stores JSON data in text form, while thejsonbtype stores JSON data in binary form. - Storage size: Since
jsonstores JSON data in text form, it requires more storage space to store the same JSON data compared tojsonb, which stores JSON data in binary form and requires less storage space. - Query performance: Since
jsonbstores JSON data in binary form, it is faster in querying compared tojson. Thejsontype requires converting JSON data to text form during querying, while thejsonbtype can directly query binary data.
Syntax
In PostgreSQL, to create a column with jsonb data type, you need to use the jsonb keyword. The jsonb type stores binary format of JSON data and has better query performance and smaller storage size compared to the json type. Here is an example SQL statement for creating a column with jsonb data type:
CREATE TABLE example_table (
id SERIAL PRIMARY KEY,
jsonb_column JSONB
);
Use Cases
The jsonb data type has a wide range of use cases in PostgreSQL, including:
- Storing unstructured data: The
jsonbdata type can store unstructured data in any format, such as logs, configuration files, and schema-less data. - Storing semi-structured data: The
jsonbdata type can store semi-structured data, such as XML documents, HTML documents, etc. - Simplifying queries: Storing data using the
jsonbdata type can simplify the query process. Using PostgreSQL’sjsonbfunctions, you can easily handle queries and aggregation operations onjsonbdata type, which are faster than queries onjsondata type. - Supporting multilingual data: The
jsonbdata type can store multilingual data, such as localized data for multilingual websites, etc.
Examples
Here are two complete examples that demonstrate how to create a column with jsonb data type and query jsonb data in PostgreSQL:
-
Creating a column with
jsonbdata typeCREATE TABLE example_table ( id SERIAL PRIMARY KEY, jsonb_column JSONB ); INSERT INTO example_table (jsonb_column) VALUES ('{"name": "John Smith", "age": 30, "address": {"city": "New York", "state": "NY"}}'); -
Querying
jsonbdataSELECT jsonb_column->>'name' AS name, jsonb_column->>'age' AS age, jsonb_column->'address'->>'city' AS city FROM example_table WHERE jsonb_column @> '{"name": "John Smith"}';Result:
name | age | city -------------+-----+---------- John Smith | 30 | New York
Conclusion
The jsonb data type is one of the most versatile types in PostgreSQL, allowing you to store structured documents, nested objects, and arrays with indexable key paths. When translating database schemas into application code, use the SQL to Types Converter to automatically map PostgreSQL jsonb columns to TypeScript Record<string, unknown>, Python dict[str, Any], and Go map[string]any. To transform raw JSON documents into PostgreSQL CREATE TABLE and batched INSERT statements, see the JSON to SQL Converter.