What is JSONB in PostgreSQL and when would you use it?

IntermediateConceptPostgreSQL
Answer

Short answer

JSONB is a PostgreSQL column type that stores JSON in a binary, indexable format. It lets you keep flexible, semi-structured data (settings, event payloads, product attributes) in a relational table and still query and index it efficiently.

Example

CREATE TABLE products (
  id serial PRIMARY KEY,
  name text NOT NULL,
  attributes jsonb
);
INSERT INTO products (name, attributes)
VALUES ('Laptop', '{"brand": "Acme", "ram_gb": 16, "tags": ["work", "travel"]}');

SELECT name, attributes->>'brand' AS brand FROM products;
SELECT * FROM products WHERE attributes @> '{"brand": "Acme"}';     -- contains
SELECT * FROM products WHERE (attributes->>'ram_gb')::int >= 16;

CREATE INDEX idx_products_attrs ON products USING GIN (attributes);  -- speeds up @> queries

Operators to know

Operator Meaning
-> Get a JSON value
->> Get a value as text
@> Contains
? Key exists

JSON vs JSONB

json stores the text as-is (slower to query); jsonb is parsed and indexable — almost always use jsonb.

When not to use it

For core fields you filter, join and constrain on all the time, use normal columns — you get types, constraints and simpler queries.

What to say in the interview

"JSONB stores binary JSON that can be queried with operators like ->> and @> and indexed with GIN. It's great for flexible attributes or event payloads alongside relational data, while core fields stay as typed columns."

All Databases (MySQL, PostgreSQL, MongoDB) questions Roadmap: PostgreSQL basics

More on PostgreSQL