PostgreSQL in Depth
PostgreSQL features beyond standard SQL: richer data types, auto-incrementing ids, and the psql client.
What is it?
SELECT, JOIN, and WHERE are standard SQL that works, with minor syntax differences, across most relational databases. PostgreSQL ("Postgres") is one of the most widely used databases, and it adds its own extensions:
- richer column types like JSONB (structured, queryable JSON stored in an efficient binary form) and arrays - auto-incrementing id columns - psql, an interactive command-line client for talking to a database directly
Explain like I'm 10
Standard SQL is like standard English, understood everywhere. Postgres features are a rich regional vocabulary: more expressive with someone who knows it, meaningless to a database that only speaks the standard dialect.
Examples
Postgres-specific column types
CREATE TABLE products (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
tags TEXT[],
metadata JSONB,
created_at TIMESTAMPTZ DEFAULT now()
);SERIAL gives an auto-incrementing integer id, TEXT[] stores a list of strings in one column, JSONB holds a flexible structured document, and TIMESTAMPTZ is a timezone-aware timestamp defaulting to now.
Querying inside a JSONB column
SELECT name, metadata->>'color' AS color
FROM products
WHERE metadata->>'in_stock' = 'true';-> extracts a nested value as JSON; ->> extracts it as plain text. ->> is used here because the color is compared and shown as ordinary text.
How it works
JSONB isn't stored as the text you typed. Postgres parses it into a decomposed binary format up front. That enables indexing (a GIN index can index the keys/values inside a JSONB column) and fast operators like -> and ->>, at the cost of a little overhead on write.
The psql client connects straight to a database (psql -h host -U user -d dbname). Commands like \dt list tables and \d products shows a table's columns, handy for inspecting data or debugging without writing a script.
Why does it exist?
Projects often mix strict relational data (a user's id and email) with flexible, per-row data (a product's optional custom attributes). JSONB lets Postgres handle both, so you don't need a separate document database just for the flexible parts.
When to use it
Use JSONB for a few truly flexible or sparse fields: custom attributes, a webhook payload, per-user settings. Use psql to inspect data, run ad-hoc queries, or debug against a database during development.
When not to use it
Don't store your whole schema as JSONB just because it's flexible. You lose the enforcement, clarity, and full indexing support of real columns and foreign keys. If a field has a known, stable shape and you query or join on it often, make it a real column.
Common mistakes
Using -> (which returns JSON) when you wanted plain text from ->>, causing confusing type-mismatch errors in comparisons.
Using a JSONB column to avoid designing a proper related table, out of laziness rather than real need for flexibility.
Assuming SERIAL ids have no gaps. Deleted rows leave permanent gaps, since the sequence never goes backward.
Practice exercises
- Easy:
Write a CREATE TABLE for an 'events' table with a SERIAL id and a JSONB 'payload' column.
- Medium:
Write a query selecting rows whose JSONB 'settings' column has a nested key 'theme' equal to 'dark'.
- Hard:
Describe one case where you'd choose a JSONB column over a proper related table, and one where you'd choose the related table.
Interview questions
What's the difference between JSON and JSONB in PostgreSQL?
JSON is stored as the exact text you inserted. JSONB is parsed into a decomposed binary form: slightly slower to write, but it supports indexing and faster queries.
What does a SERIAL column do?
It creates an auto-incrementing integer column, backed by a hidden sequence that generates each new row's id.
When would you reach for a JSONB column instead of normalizing into another table?
When the data's shape really varies or is sparse per row, and you don't query or join on it much. Otherwise a related table usually fits better.