PostgreSQL flashcards
The open source relational database that does it all.
Beginner friendly, starts from zero
0
PostgreSQL at a glance
PostgreSQL is a free database that keeps an app's data in tables and answers questions about it.
Like a strict, fast librarian: every fact on a labelled shelf, and any record found the moment you ask.
Words you will meet
- Table
- A grid of data, like one sheet in a spreadsheet
- Row
- One record in a table, such as one customer
- Column
- One kind of fact in a table, such as the city
- Query
- One question or command you send to the database
- Primary key
- The column that gives each row its own unique id
- Foreign key
- A column that points to a row in another table
- NULL
- No value or unknown, not the same as zero
- Index
- A shortcut that helps the database find rows fast
The steps
- 1. Start from zero (12 cards): What a database is, why it exists, and the words you need before Core basics.
- 2. Core basics (17 cards): psql, tables, types, keys and the everyday queries you run on day one.
- 3. Next steps (16 cards): Transactions, indexes, query plans, JSONB, upserts, roles and backups.
- 4. Go deeper (18 cards): MVCC, vacuum, locking, isolation, partitioning, replication and tuning at scale.
All 12 cards in Start from zero
- What is PostgreSQL, in one plain sentence?
- PostgreSQL (often called Postgres) is a free program that stores an app's data in tables and answers questions about it, safely, for many people at once.
- Before databases, how did a shop keep track of thousands of orders?
- In ledgers, spreadsheets or loose files. Two people editing at once overwrote each other, files got damaged, and finding one order among millions took ages.
- Where do you already meet a database like PostgreSQL in daily life?
- Your bank balance, a train ticket booking, your order history in a shopping app and your saved address are all rows in a database behind the app.
- What is a good everyday picture of a database?
- A very strict, very fast librarian. Every fact goes on a labelled shelf, he refuses anything that breaks the rules, and he finds any record the moment you ask.
- What are a table, a row and a column?
- A table is like one sheet in a spreadsheet, such as customers. Each row is one record (one customer) and each column is one kind of fact (name, city). Relational means tables can link to each other.
- Your first tiny win: what does this SQL ask?
- SQL is the language you talk to the database in, and one question is a query.
SELECTpicks which columns,FROMnames which table, andWHEREkeeps only which rows match. - What do server, client and connection mean for a database?
- PostgreSQL runs as a server, a program that waits for questions. You reach it with a client such as
psql, opening a connection with a user name, a password and a database name. - What is a data type, and what does NULL mean?
- Each column has a data type: text, a whole number, a decimal, a date and time, or true and false. NULL means no value or unknown, which is not the same as zero or empty text.
- What are a primary key and a foreign key?
- A primary key is a column that gives each row its own unique id, like a roll number in a class. A foreign key is a column that points to a row in another table, like an order pointing to its customer.
- What are a constraint and an index?
- A constraint is a rule the database enforces, such as 'price cannot be negative'. An index is like the index at the back of a book: it lets the database find rows fast without reading every one.
- What do you need before you start?
- Nothing to install to begin: read the cards and their example results. To try queries yourself, install PostgreSQL 18 on your computer, which also gives you the
psqltool. - After this topic, what will you be able to do?
- Connect to a database, create tables that refuse bad data, add, change and remove rows, join tables together and build simple reports with counts and totals.
PostgreSQL cheatsheet
psql
\l \c db \dnDatabases, connect, schemas\dt \d+ ordersTables, full table detail\di \df \duIndexes, functions, roles\x autoExpanded view for wide rows\timing onShow query time\eEdit last query in$EDITOR\copy t TO 'out.csv' CSV HEADERExport from the client side\watch 2Rerun the last query every 2 s
Postgres SQL
INSERT ... RETURNING idGet generated values backON CONFLICT (k) DO UPDATE SET v = EXCLUDED.vUpsertSELECT DISTINCT ON (k) ... ORDER BY k, ts DESCLatest row per keyFOR UPDATE SKIP LOCKEDJob queue pickgenerate_series('2026-10-01'::date, '2026-10-31', '1 day')Fill date gapsa IS DISTINCT FROM bNULL-safe comparex::int / now() - interval '7 days'Cast, date maths
JSONB and arrays
data->'a' / data->>'a'Get asjsonb/ astextdata #>> '{a,b}'Nested path as textdata @> '{"k":"v"}'Contains (GIN-indexable)data ? 'k'Key existsjsonb_set(data, '{a}', '1')Change one keydata - 'k'Remove a keytags @> ARRAY['rust']Array contains
Performance
EXPLAIN (ANALYZE, BUFFERS)Real plan with I/OCREATE INDEX CONCURRENTLYBuild without blocking writesCREATE INDEX ... WHERE condPartial indexUSING gin (col jsonb_path_ops)FastjsonbcontainmentUSING brin (created_at)Tiny index for append-only timepg_stat_statementsTop queries by total timeANALYZE ordersRefresh planner statistics
Admin and safety
SET lock_timeout = '3s'Fail fast in migrationsALTER ROLE r SET statement_timeout = '5s'Cap query timepg_dump -Fc / pg_restore -j 8Backup and parallel restoreSELECT pg_cancel_backend(pid)Cancel a querySELECT pg_terminate_backend(pid)Kill a sessionpg_stat_activity WHERE state <> 'idle'What is running nowSELECT pg_reload_conf()Reload config, no restart