📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials PostgreSQL Essentials JSONB Columns

JSONB Columns

6 min read Quiz at the end
JSONB stores JSON as binary for fast querying. Use -> to access a key as JSON and ->> as text. The @> containment operator checks if a JSONB column includes a given value. Add a GIN index for fast JSONB queries.

Working with JSONB

CREATE TABLE products (
  id    SERIAL PRIMARY KEY,
  meta  JSONB
);

INSERT INTO products (meta)
VALUES ('{"color":"red","size":42}');

-- Query JSONB
SELECT meta->>'color' FROM products;
SELECT * FROM products WHERE meta @> '{"color":"red"}';

-- Index JSONB
CREATE INDEX idx_meta ON products USING GIN(meta);
Topic Quiz · 5 questions

Test your understanding before moving on

1. What operator queries JSONB containment?
💡 @> checks if a JSONB value contains another: WHERE meta @> '{"color":"red"}'.
2. What does ->> return?
💡 ->> returns a JSON field value as text (vs -> which returns JSONB).
3. Which index type works best with JSONB?
💡 GIN (Generalized Inverted Index) is ideal for JSONB columns.
4. What does jsonb_pretty() do?
💡 jsonb_pretty() returns a JSON value formatted with indentation for readability.
5. Which is better for querying: JSON or JSONB?
💡 JSONB is stored in binary format supporting indexing — much faster to query.