PostgreSQL Reference

This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.

Welcome to the PostgreSQL reference. PostgreSQL is an open-source object-relational database: it stores data in typed tables, speaks SQL with a very broad implementation of the standard plus its own extensions (arrays, ranges, jsonb, full-text search, user-defined types), guarantees ACID transactions through multiversion concurrency control and a write-ahead log, and grows through extensions such as PostGIS and pgvector. This section is written for developers and engineers who build on PostgreSQL or run it: it explains how the server works, the SQL and index features that are specific to it, how to read query plans, how concurrency behaves, and how to administer, back up, replicate and tune a deployment. It is verified against the PostgreSQL documentation.

If you are new to PostgreSQL, start with Getting Started, which runs a server in Docker, introduces psql and loads the shop dataset used by every page. Engine-agnostic SQL (DDL, queries, joins, window functions) is documented once in the sibling SQL Reference, which these pages link to rather than repeat.

Version baseline

This section documents PostgreSQL 18, the current major version, and every example was executed against PostgreSQL 18.6 from the official postgres:latest Docker image (Debian build, arm64). Where a feature depends on the version, the page says which release introduced it: for example MERGE (15), JSON_TABLE and MERGE ... RETURNING (17), and virtual generated columns, uuidv7(), skip scans on multicolumn B-tree indexes and OLD/NEW in RETURNING (18). The community releases one major version a year, supports each for five years and publishes minor releases quarterly; see the versioning policy and the release notes.

What’s covered

Getting started

  • Getting Started — what PostgreSQL is and how it is distributed, running it in Docker, psql basics, connecting, loading the shop seed (shown in full), a first session, and a version timeline of the features the section uses.

Internals

  • Architecture — the postmaster and per-connection backend processes, the background processes, shared buffers and the OS cache, the cluster and PGDATA layout, heap pages and TOAST, MVCC row versions (xmin / xmax) inspected with pageinspect, the write-ahead log, checkpoints and crash recovery.

Data types, SQL and JSON

  • Data Types & PostgreSQL-specific SQL — numeric, text and timestamp types, arrays, enums, ranges, domains, identity and generated columns, RETURNING, upserts with ON CONFLICT, MERGE, DISTINCT ON, LATERAL, FILTER, CTEs and MATERIALIZED, COPY, and the psql meta-commands that script SQL.

  • JSON & JSONB — the json and jsonb types, the jsonb operators, jsonpath, indexing jsonb, and the SQL/JSON functions.

Indexing and query planning

  • Indexing — B-tree (multicolumn, covering, partial and expression indexes), hash, GIN, GiST, SP-GiST and BRIN, unique and exclusion constraints, CREATE INDEX CONCURRENTLY, index-only scans and the visibility map, REINDEX and bloat, and full-text search basics, each with EXPLAIN evidence on generated data.

  • Query Planning & EXPLAIN — the planner stages, statistics and extended statistics, EXPLAIN options and how to read scan, join and aggregate nodes, the cost model, the enable_* switches for diagnosis, generic versus custom plans, parallel query and partition pruning.

Transactions and concurrency

  • Transactions, Isolation & MVCC — snapshots, READ COMMITTED, REPEATABLE READ and SERIALIZABLE (SSI) with real two-session demonstrations, write skew and serialization-failure retry, row and table locks, SKIP LOCKED queues, advisory locks, deadlocks, pg_locks and pg_stat_activity, savepoints and the hazards of idle-in-transaction sessions.

Operations

  • Administration — roles and privileges, row-level security, pg_hba.conf, the configuration files and ALTER SYSTEM, VACUUM and autovacuum, the monitoring views, and routine maintenance.

  • Backup & Restore — pg_dump, pg_restore and pg_dumpall, physical base backups, WAL archiving, and point-in-time recovery.

  • Replication & High Availability — streaming and logical replication, replication slots, failover, an overview of Patroni, and connection pooling with pgBouncer.

  • Performance Tuning — memory settings, checkpoint tuning, pg_stat_statements usage, connection pooling, partitioning and bulk loading.

Extensions and reference

  • Extensions — how extensions are installed and managed, and a tour of pg_stat_statements, pgvector, PostGIS and other commonly used ones.

  • Cheat Sheet — a one-page quick reference to the commands and queries of this section.

Deliberately out of scope. This section is a reference for the PostgreSQL server, its SQL and its operation, not a tutorial for one framework, installer or cloud product. The following topics appear in most books on the subject but live elsewhere on this site or are intentionally not covered:

  • Installers and packages for each operating system — the download page is authoritative; this section runs the server in a container, see Getting Started and Docker.

  • PL/pgSQL internals beyond the basics, procedural languages and writing your own extension — procedural code is introduced in Triggers & Procedures; writing a C extension is not covered.

  • Rules, event triggers and logical replication of DDL — row-level triggers are in the SQL pages; the legacy rule system and event triggers are not covered, and logical replication is described for data changes only in Replication & High Availability.

  • Sharding and distributed PostgreSQL (Citus and similar) — intentionally not covered beyond a mention in Extensions; table partitioning inside one server is in Performance Tuning.

  • Cloud-managed services (Amazon RDS and Aurora, Azure, Google Cloud SQL and AlloyDB) — see the cloud sections of this site, for example PostgreSQL and MySQL on Azure and AlloyDB for PostgreSQL.

  • Major-version upgrades with pg_upgrade, log analysis tools (pgBadger) and auditing frameworks — only mentioned; the pg_upgrade manual page is the reference.

  • Migrating from other databases (pgloader, Debezium) — see Database Migration and, for a complete walk-through, Worked Example: Oracle to PostgreSQL.

  • Object-relational mapping and application frameworks — see Hibernate and Spring Data JPA.

Reading path

Developers can stop after the first seven pages; operators should continue through administration, backup, replication and tuning. The cheat sheet is meant to be kept open next to a psql session.

Running example

The worked examples share one running example, a small online shop in a schema named shop: four tables with 4 customers, 5 products, 8 orders and 11 order lines. The full seed script and the Docker commands to load it are on Getting Started; the same data set backs the MongoDB Reference, so the two engines can be compared on identical data. Pages that need volume (indexing, planning, locking) generate extra rows with generate_series in a scratch schema instead of changing the shop.

erDiagram customers ||--o{ orders : places orders ||--|{ order_items : contains products ||--o{ order_items : "appears in" customers { text id PK text name text email text city text tier timestamptz created_at } products { text sku PK text name text category numeric price integer stock text_array tags jsonb attrs } orders { integer id PK text customer_id FK text status timestamptz order_date } order_items { integer order_id PK text sku PK integer qty numeric price }

A customer places orders (status is open, shipped or cancelled); an order has one or more lines in order_items, each pointing at a product by sku with a quantity and the price at the time of purchase. products carries a tags array and an attrs jsonb document, which give the array, GIN and JSON examples something to work on. The outputs on the pages were produced by running the code on PostgreSQL 18.6 in a container, so every example can be reproduced step by step.

Bibliography