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);