Database standards for PostgreSQL persistence and Redis caching. Use when designing schemas, writing migrations, optimizing queries, configuring Redis, or implementing cache invalidation.
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 Database?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/ndisisnd-database)
---
name: database
description: Database standards for PostgreSQL persistence and Redis caching. Use when designing schemas, writing migrations, optimizing queries, configuring Redis, or implementing cache invalidation.
metadata:
triggers:
files:
- '**/*.entity.ts'
- 'prisma/schema.prisma'
- 'prisma/migrations/*/migration.sql'
- '**/migrations/*.sql'
- 'drizzle.config.ts'
- 'drizzle/**/*.sql'
- '**/redis.config.ts'
keywords:
- postgres
- postgresql
- prisma
- TypeOrmModule
- PrismaService
- PostgresModule
- redis
- ttl
- eviction
targets: "PostgreSQL 16+ (17/18 features flagged) · Redis 7.2+ / Valkey 8+"
last_reviewed: 2026-10-18
---
# Database Standards
Targets: PostgreSQL 16+ (17/18-only features are flagged) · Redis 7.2+ / Valkey 8+.
Supabase-specific rules (`auth.uid()`, `service_role`, Supavisor) live in `supabase`.
## Priority: P0 — PostgreSQL Correctness
### Schema & Access
- Isolate persistence behind repositories or database services; do not scatter raw database access across handlers.
- Enforce integrity in the database: primary keys, foreign keys, `NOT NULL`, `CHECK`, `UNIQUE`.
- Use `timestamptz`, `text`, `numeric` or integer minor units for money, and `GENERATED ALWAYS AS IDENTITY` or UUIDs for keys (see [postgresql-anti-patterns](refs/postgresql-anti-patterns.md)).
### Migrations
- Use explicit, version-controlled migrations for every schema change. ORM schema sync is dev-only:
- TypeORM: never `synchronize: true` outside local dev.
- Prisma: `prisma migrate deploy` in CI/prod; `migrate dev` and `db push` are dev-only.
- Drizzle: `drizzle-kit generate` + `migrate` for shared environments; `drizzle-kit push` is prototyping-only.
- Destructive or renaming changes use expand-contract — the recipe is [postgresql-implementation](refs/postgresql-implementation.md).
- On live tables, set `lock_timeout`, build indexes `CONCURRENTLY`, and add constraints `NOT VALID` then `VALIDATE` — see the migration section of [postgresql-checklist](refs/postgresql-checklist.md).
- Generated ORM migrations do not capture Row-Level Security. Write RLS as explicit SQL.
### Queries & Performance
- Paginate every list query (keyset for deep pages).
- Index every hot filter, join, foreign-key, and RLS-predicate column.
- Use short transactions for multi-step mutations. Prevent N+1 with joins or batching.
- Project only the columns you need; no `SELECT *` in application queries.
- Behind a transaction-mode pooler (PgBouncer, Supavisor), do not rely on session state — see the pooling section of [postgresql-best-practices](refs/postgresql-best-practices.md).
## Priority: P0 — Redis Safety
### Data Design
- Redis/Valkey is a cache, queue, or coordination layer — never the sole source of truth for critical business data.
- Namespace keys with colons (`app:user:123`). Every non-permanent key needs a TTL; add TTL jitter on high-volume keys.
- Prefer hashes for field-addressable objects over large JSON blobs.
### Commands & Operations
- Never use `KEYS` in production; use `SCAN`. Use `UNLINK` for large keys. Bound every range read.
- Use pipelines for bulk work; Redis Functions (`FCALL`) or Lua for atomic multi-step logic.
- In production use ACL users, TLS, and disabled dangerous commands.
## Anti-Patterns
- Session-level `SET`, advisory locks, or SQL `PREPARE` through a transaction-mode pooler.
## References
Load only what the task requires:
PostgreSQL:
- [postgresql-implementation](refs/postgresql-implementation.md) — expand-contract migration recipe
- [postgresql-best-practices](refs/postgresql-best-practices.md) — indexing, pooling, RLS, partitioning, locking, diagnostics, PG17/18
- [postgresql-checklist](refs/postgresql-checklist.md) — review checklist incl. safe migrations
- [postgresql-anti-patterns](refs/postgresql-anti-patterns.md) — types, constructs, operations to avoid
- [sql-gotchas](refs/sql-gotchas.md) — `UPDATE ... FROM`, timezone, `NULL`, `ILIKE`, `JSONB`
Redis / Valkey:
- [redis-best-practices](refs/redis-best-practices.md) — caching, memory, commands, clients, security, licensing
- [redis-checklist](refs/redis-checklist.md) — Redis setup and cache review checklist