Postgres by Example
Dariush Abbasi
English edition · PostgreSQL 17 target
Fifty-seven short lessons take you from the first query through data modeling, querying, indexes, transactions, stored logic, search, and day-to-day operations. Each lesson includes a SQL file; the backup lesson documents shell commands, and the psql reference includes catalog-query alternatives.
Start reading · Print the whole book · Source repository
Prerequisites
The lessons target PostgreSQL 17 and use the psql command-line client.
Later-version features are identified explicitly. The
official documentation is a useful companion.
Download the complete SQL bundle and extract it into
a new directory. Run commands from the directory containing source/. Use a
disposable database: these lessons create, change, and drop their demo tables.
The examples show the database name postgres; substitute your own practice
database, such as pbe:
Read in chapter order. Before chapter 6, create the sample table using the files introduced in chapters 16 and 24:
Chapter 16 recreates the table; chapter 24 fills it again. Later join lessons depend on chapter 29, and the recursive example also uses chapter 31. When skipping ahead, follow the lesson’s prerequisites. Optional exercises can change later results. Dates, UUIDs, catalog listings, query plans, and timings vary; ellipses in console transcripts mark omitted output.
Chapter 46 deliberately raises a duplicate-key error: run it with
ON_ERROR_STOP=off so the subsequent rollback and cleanup execute.
For chapter 55, use a non-superuser database owner without BYPASSRLS;
superusers bypass row security even with FORCE ROW LEVEL SECURITY.
Contents
Examples
- Example 1.1 — First Query
- Example 2.1 — psql Basics
- Example 3.1 — SELECT Basics
- Example 4.1 — WHERE
- Example 5.1 — ORDER BY
- Example 6.1 — SELECT from a Table
- Example 7.1 — LIMIT and OFFSET
- Example 8.1 — DISTINCT
- Example 9.1 — NULLs
- Example 10.1 — Expressions
- Example 11.1 — Numeric Types
- Example 12.1 — Text Types
- Example 13.1 — Boolean and Dates
- Example 14.1 — UUID and JSONB
- Example 15.1 — Arrays and ENUM
- Example 16.1 — CREATE TABLE
- Example 17.1 — Column Types and Constraints
- Example 18.1 — ALTER TABLE and DROP
- Example 19.1 — Primary Keys and UNIQUE
- Example 20.1 — Foreign Keys and REFERENCES
- Example 21.1 — NOT NULL and DEFAULT
- Example 22.1 — Identity Columns and Sequences
- Example 23.1 — Schemas and search_path
- Example 24.1 — INSERT
- Example 25.1 — UPSERT (ON CONFLICT)
- Example 26.1 — UPDATE
- Example 27.1 — DELETE
- Example 28.1 — RETURNING
- Example 29.1 — INNER and LEFT JOIN
- Example 30.1 — RIGHT and FULL JOIN
- Example 31.1 — Self-Join
- Example 32.1 — UNION, INTERSECT, EXCEPT
- Example 33.1 — COUNT, SUM, AVG
- Example 34.1 — GROUP BY
- Example 35.1 — HAVING
- Example 36.1 — Scalar and IN Subqueries
- Example 37.1 — EXISTS and Derived Tables
- Example 38.1 — Common Table Expressions (WITH)
- Example 39.1 — Window Functions
- Example 40.1 — String and Numeric Functions
- Example 41.1 — Date Functions and COALESCE
- Example 42.1 — CASE
- Example 43.1 — CREATE INDEX
- Example 44.1 — When to Index
- Example 45.1 — EXPLAIN and Query Plans
- Example 46.1 — BEGIN, COMMIT, ROLLBACK
- Example 47.1 — Savepoints
- Example 48.1 — CREATE VIEW
- Example 49.1 — Materialized Views
- Example 50.1 — Functions and PL/pgSQL
- Example 51.1 — Triggers
- Example 52.1 — Full-Text Search
- Example 53.1 — VACUUM and ANALYZE
- Example 54.1 — pg_dump and pg_restore
- Example 55.1 — Roles and GRANT
- Example 56.1 — psql Meta-commands
- Example 57.1 — COPY
About this edition
This English web edition follows the boringcollege/postgres-by-example
repository at commit ffde0d358722498e37982b57bdbd711871a61c43. The lesson
prose and SQL examples are kept together, with reviewed technical corrections
documented in the editorial notes. The repository is licensed under
Creative Commons Attribution 4.0 International.
Publication and license · Editorial corrections