Before Your First Shift#
Never run PostgreSQL or pgdorm on your machine before? Sixty seconds from now you'll have a live PostgreSQL container humming on localhost and your first typed query running — no repo clone required.
60-Second Single-File Quickstart#
Requirements: Python 3.15+ (abi3t free-threaded wheels) and PostgreSQL 18 or newer (Docker, OrbStack, Colima, or local install).
1. Install Python 3.15 + pgdorm and start PostgreSQL#
uv python install 3.15
uv init my-pgdorm-app --python 3.15 && cd my-pgdorm-app
uv add pgdorm
docker run -d --name dorm-pg -e POSTGRES_PASSWORD=dorm -p 5432:5432 postgres:18-alpine
export DATABASE_URL="postgresql://postgres:dorm@localhost:5432/postgres"
2. Define a pg.Table model and run your first query#
class Guest(pg.Table):
guest_id: pg.Col[int] = pg.pk(default=pg.identity())
email: pg.Col[str] = pg.col(unique=True)
full_name: pg.Col[str] = pg.col()
await pool.create_all(Guest)
jumbo = await pool.one(
pg.insert(Guest)
.values(Guest(email="[email protected]", full_name="Jumbo Kalahari"))
.returning(Guest)
)
found = await pool.one(pg.select(Guest).where(Guest.guest_id == jumbo.guest_id))
uv run python app.py
# => Checked in #1: Jumbo Kalahari <[email protected]>
The 5 House Rules (and Feed /pgdorm/llms.txt to Your AI Agent)#
pgdorm enforces strict PostgreSQL guardrails at class-definition and query-build time. If you use Cursor, Claude Code, or Antigravity, feed it /pgdorm/llms.txt (or /pgdorm/llms-full.txt) so it follows these five rules on the first try:
- Singular
snake_casetable names:class OrderItem(pg.Table)maps to"order_item". Passingname="order_items"is forbidden (SchemaLintError). - Own-PK
<table_name>_id: Every permanent table declares<table_name>_id(guest_id, notid). Binding a legacy database withid? Passstrict_pk=False(or usepgdorm scaffold --mode bind). TypeTableclasses end inType: Useclass RoomStatusType(pg.TypeTable)backed by anIntEnum— string discriminators and unvalidatedstatus: Col[str]enums are forbidden.- Zero raw SQL execution: Never call
pool.execute("SELECT ...")orpg.raw(...)— they do not exist. Compose everything withpg.select,pg.insert,pg.update,pg.delete,pg.func,pg.cast, andpg.lit. - Explicit, indexed joins &
= ANY($1):JoinLintErrorblocks ambiguous or unindexed foreign-key joins (opt out explicitly with.allow_seq_scan(reason="...")), and membership filters useCol.in_any(values)so PostgreSQL caches one prepared statement forever.
Hit a linter or runtime error? Jump to House Rules & Error Fixes or look up any signature in the API Reference.
Optional: Clone the Dormitory Repo to Run the 20-Chapter Book#
Want to run all 20 standalone tutorial chapters from The Elephant Dormitory locally?
Requirements#
- Python 3.15+ (minimum version declared in
pyproject.toml; ships free-threadedabi3twheels) - PostgreSQL 18 or newer (run
pgdorm doctorto verify server compatibility) uv(recommended Python package manager)- Rust toolchain (
rustup/cargo, minimum edition inCargo.toml) — only needed when building from source inside a repo clone
Clone and build the Rust extension#
git clone https://gitlab.com/wergstatt/pgdorm.git
cd pgdorm
uv sync --group dev --group docs
uv run --no-sync maturin develop
Run Chapter 1 live (from your clone)#
Every chapter in The Elephant Dormitory is a standalone Python file in examples/. Each script drops and recreates only its own ex_<slug> schema, so running it repeatedly is always clean:
You should see Jumbo Kalahari check in, get looked up, upgrade to SILVER, and check out — matching every highlighted # => line in Chapter 1.
Verify the whole book#
Want to run all 20 tutorial chapters against your local PostgreSQL and check every output and wire SQL snapshot? One advanced chapter needs a non-superuser role for Row-Level Security (Chapter 14). Normal pgdorm apps don't need it; create it once to run the full suite:
# Optional: only needed to run the RLS chapter (Ch. 14)
docker exec -i dorm-pg psql -U postgres -c "CREATE ROLE pgdorm_rls_user NOLOGIN NOBYPASSRLS;"
uv run --no-sync pytest tests/docs/test_examples.py -v
Ready? It's Friday, 23:45 in Lisbon, and a woolly mammoth just crashed the spreadsheet.
Next →House Rules & Error FixesSymptom-to-Fix Hub for Lint & Runtime Errors