Databases

PostgreSQL

A powerful open source relational database with extensions, JSONB, full-text search and strong SQL support.

What it is

PostgreSQL descends from the POSTGRES project at Berkeley. Today it is an open database governed by a community with no single owner. It is known for reliability, ACID transactions and closeness to the SQL standard.

Besides classic tables it handles JSONB, arrays, full-text search, geodata (PostGIS), vector search (pgvector) and custom types and extensions. Concurrency uses MVCC: readers do not block writers.

It has streaming and logical replication, partitioning, window functions and CTEs. It is usually the best default choice for a new web project.

Use it when

  • The main database of a web app, SaaS or financial data where integrity matters.
  • Complex queries, reports, JSON next to relational data.
  • Geodata or vector search through extensions.

Avoid it when

  • Huge event streams and analytics over billions of rows: consider ClickHouse.
  • Horizontal write scaling without complex setups: look at Cassandra or CockroachDB.

Key concepts

MVCC
Row versioning so reads and writes do not block each other.
VACUUM
Cleaning old row versions and updating statistics.
Index types
B-tree, GIN, GiST, BRIN for different queries.
Extensions
PostGIS, pgvector, pg_trgm and others.
WAL
The write-ahead log for recovery and replication.

Example

A table with JSONB, an index and an aggregation.

Cheat sheet

PostgreSQL cheat sheet
Command or codeWhat it does
psql -h localhost -U app -d mydbConnect to a database
\dtList tables in psql
\d usersTable structure: columns, indexes, keys
CREATE INDEX ON users (email);Create a B-tree index
EXPLAIN ANALYZE SELECT * FROM users WHERE id = 1;Query plan with real timings
SELECT data->>'name' FROM events;Extract a jsonb field as text
WITH t AS (SELECT ...) SELECT * FROM t;CTE: a named subquery
BEGIN; UPDATE ...; COMMIT;Transaction: all or nothing
pg_dump -Fc mydb > mydb.dumpBack up a database in compressed format
pg_restore -d mydb mydb.dumpRestore a database from a dump

How to learn it

  1. Install PostgreSQL (locally or in Docker) and connect with psql: create a database and a table, then run INSERT and SELECT.
  2. Learn SQL from scratch: JOIN, GROUP BY, subqueries, CTEs, window functions. The SQL course on ShortLesson helps.
  3. Get the data types right: jsonb, uuid, timestamptz, arrays, numeric for money.
  4. Design a schema of 4 to 5 tables with primary and foreign keys, constraints and migrations.
  5. Add indexes (B-tree, GIN) and read EXPLAIN ANALYZE to see the query plan change.
  6. Understand transactions, isolation levels, locking and MVCC, and reproduce a deadlock with two sessions.
  7. Set up backups (pg_dump, WAL), a least privilege role and connection pooling (PgBouncer).
  8. Try replicas, partitioning and extensions (pg_trgm, PostGIS, pgvector) on a real task.

Interview questions

  1. 1. What is MVCC?

    Multi-version concurrency control: each transaction sees a snapshot of data and changes create new row versions. That is why reads do not block writes.

  2. 2. When to use JSONB instead of separate columns?

    For semi-structured or variable attributes. For data you filter and join on often, regular typed and indexed columns are better.

  3. 3. Why is an index needed and what does it cost?

    An index speeds up lookups and sorting but takes space and slows inserts and updates. EXPLAIN ANALYZE shows whether the planner uses it.

  4. 4. What does VACUUM do?

    It removes dead row versions left by MVCC and frees space for reuse. Autovacuum runs it automatically.

Interview questions from the bank

Comparisons with this technology

Logos belong to their owners and are used for identification only. Icons: Simple Icons (CC0), Devicon (MIT).