Home › PostgreSQL › PostgreSQL schema & SQL AGENTS.md

PostgreSQL schema & SQL AGENTS.md

AGENTS.md for working on PostgreSQL 16+ schemas and SQL: safe migrations, indexes, constraints, and query plans.

AGENTS.mdPostgreSQL

How to use

Save this as AGENTS.md in your repository root, commit it, and your agent will pick it up automatically. Edit the commands to match your project.

AGENTS.mdDownload  
# AGENTS.md: PostgreSQL work

## Migrations
- Every change is a new migration file in the repo's tool (`migrations/`, Flyway, sqitch, Prisma, etc.). Never edit an applied migration.
- Make migrations safe on live tables:
  - `CREATE INDEX CONCURRENTLY` (outside a transaction) for big tables.
  - Add columns nullable or with a constant default; backfill in batches; then add `NOT NULL` (via a `CHECK ... NOT VALID` + `VALIDATE CONSTRAINT`).
  - Rename or drop in two deploys: stop using it first, remove later.
  - Set `lock_timeout` (e.g. `SET lock_timeout = '5s'`) in migrations that take locks.

## Schema
- Primary keys `bigint generated always as identity` or `uuid` (v7 if available); be consistent with the existing tables.
- `timestamptz` for time, `text` over `varchar(n)`, `numeric` for money, `jsonb` only for truly unstructured data.
- Enforce rules in the database: `NOT NULL`, `UNIQUE`, `CHECK`, foreign keys with an explicit `ON DELETE`.
- Index every foreign key column used in joins.

## Queries
- Parameterized queries only. Check new queries with `EXPLAIN (ANALYZE, BUFFERS)` on realistic data and paste the plan in the PR.
- Paginate with keyset (`WHERE id > $1 ORDER BY id LIMIT 50`) instead of large `OFFSET`.
- No `SELECT *` in application code.

## Never
Run `DROP`, `TRUNCATE` or an unbounded `DELETE`/`UPDATE` against a shared database without explicit approval.

Related rules

REST API design AGENTS.md

AGENTS.md for designing consistent HTTP/JSON APIs: resource naming, status codes, errors, pagination, and versioning.

AGENTS.mdAPI design

Bevy game engine AGENTS.md

AGENTS.md for Bevy (0.16+) games in Rust: ECS systems, plugins, required components, and fast dev builds.

AGENTS.mdBevy (Rust)

Git commits & PRs AGENTS.md

AGENTS.md rules for how agents commit: Conventional Commits, atomic changes, safe git commands, and PR descriptions.

AGENTS.mdAgent workflows

CrewAI multi-agent AGENTS.md

AGENTS.md for CrewAI 1.x projects: YAML agents and tasks, crews and flows, tools, and cost-aware runs.

AGENTS.mdLLM apps & agents

Get new rules weekly

One email a week: fresh rule files, agent tips, and what's changing in Cursor, Claude Code and Copilot. Free, unsubscribe anytime.

Opens the Rulestack Newsletter on beehiiv in a new tab. Or subscribe directly on beehiiv.