Use when writing, reviewing or optimizing PostgreSQL schema, queries or indexes. Integrity in the database, indexes that match queries, EXPLAIN before shipping, no per-item queries.
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Postgres Best Practices?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/mvbsoft-postgres-best-practices)
---
name: postgres-best-practices
description: Use when writing, reviewing or optimizing PostgreSQL schema, queries or indexes. Integrity in the database, indexes that match queries, EXPLAIN before shipping, no per-item queries.
---
# PostgreSQL best practices
## Easy to miss
- Moving existing rows with their ids: use `GENERATED BY DEFAULT AS IDENTITY` (ALWAYS rejects explicit
ids) and `setval` the sequence after the copy.
- Never ship a schema file nobody ran: apply it to a throwaway database, or say clearly that it was
not run.
- **Integrity first:** enforce invariants in the database: `NOT NULL`, foreign keys, `UNIQUE`,
`CHECK`. Normalize by default; denormalize only with a measured reason.
- **Types:** `timestamptz`, never naive timestamps; `bigint` or `uuid` surrogate keys with natural
`UNIQUE` constraints; real columns over JSON unless the shape is truly dynamic.
- **Indexes:** on columns used in `WHERE`, `JOIN` and `ORDER BY`; composite column order matches the
predicates; partial or expression indexes where queries filter that way; don't over-index tables
with heavy writes.
- **Verify:** `EXPLAIN (ANALYZE, BUFFERS)` for a hot query: no full scan on a large table, sane row
estimates, no nested-loop blow-up.
- **No per-item queries:** batch with `IN`, `ANY` or joins; use the ORM's eager loading.
- **JSONB:** `jsonb`, not `json`; GIN index on queried paths; don't hide relational data in JSON.
- **Vector search (pgvector):** choose `hnsw` or `ivfflat` deliberately and tune recall versus
latency; results are approximate, so validate them.
- **Connections and transactions:** use pooling; keep transactions short; never hold a connection
across an external call.
- **Migrations:** see `migration-safety`.