Skip to content

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#

app.py · model
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()
app.py · create_all + insert + select
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:

  1. Singular snake_case table names: class OrderItem(pg.Table) maps to "order_item". Passing name="order_items" is forbidden (SchemaLintError).
  2. Own-PK <table_name>_id: Every permanent table declares <table_name>_id (guest_id, not id). Binding a legacy database with id? Pass strict_pk=False (or use pgdorm scaffold --mode bind).
  3. TypeTable classes end in Type: Use class RoomStatusType(pg.TypeTable) backed by an IntEnum — string discriminators and unvalidated status: Col[str] enums are forbidden.
  4. Zero raw SQL execution: Never call pool.execute("SELECT ...") or pg.raw(...) — they do not exist. Compose everything with pg.select, pg.insert, pg.update, pg.delete, pg.func, pg.cast, and pg.lit.
  5. Explicit, indexed joins & = ANY($1): JoinLintError blocks ambiguous or unindexed foreign-key joins (opt out explicitly with .allow_seq_scan(reason="...")), and membership filters use Col.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-threaded abi3t wheels)
  • PostgreSQL 18 or newer (run pgdorm doctor to verify server compatibility)
  • uv (recommended Python package manager)
  • Rust toolchain (rustup / cargo, minimum edition in Cargo.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:

uv run --no-sync python examples/097_hello_dorm.py

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.