PostgreSQL + Drizzle ORM Best Practices
Type-safe database applications with PostgreSQL 18 and Drizzle ORM.
Directory Structure
src/db/
├── schema/
│ ├── index.ts # Re-export all tables
│ ├── users.ts # User table + relations
│ └── posts.ts # Post table + relations
├── db.ts # Database connection
└── migrate.ts # Migration runner
drizzle/
└── migrations/ # Generated SQL migrations
drizzle.config.ts # drizzle-kit configuration
Essential Commands
npx drizzle-kit generate # Generate migration from schema
npx drizzle-kit migrate # Apply migrations
npx drizzle-kit push # Push schema directly (dev only)
npx drizzle-kit studio # Database browser
PostgreSQL 18 Highlights
| Feature |
Benefit |
| UUIDv7 |
Timestamp-ordered UUIDs, better index performance |
| Async I/O |
Up to 3x faster sequential scans |
| Index Skip Scan |
~40% faster queries on composite indexes |
| RETURNING OLD/NEW |
Access previous values in UPDATE/DELETE |
Performance Checklist
Code Examples
Complete, runnable examples in the examples/ directory:
TypeScript (Drizzle ORM)
| File |
Description |
| Schema |
|
examples/typescript/schema/users.ts |
User table with timestamps, soft delete |
examples/typescript/schema/posts.ts |
Posts with foreign key, indexes |
examples/typescript/schema/many-to-many.ts |
Junction table pattern |
examples/typescript/schema/enums-jsonb.ts |
Enums and typed JSONB |
| Queries |
|
examples/typescript/queries/select.ts |
Filters, pagination, search |
examples/typescript/queries/joins.ts |
Left/inner/multiple joins |
examples/typescript/queries/aggregations.ts |
Count, sum, group by |
examples/typescript/queries/mutations.ts |
Insert, update, delete, upsert |
examples/typescript/queries/transactions.ts |
Transactions, savepoints |
| Relations |
|
examples/typescript/relations/relational-queries.ts |
Nested queries, column selection |
| Migrations |
|
examples/typescript/migrations/drizzle.config.ts |
drizzle-kit configuration |
examples/typescript/migrations/migrate.ts |
Programmatic migration runner |
examples/typescript/migrations/seed.ts |
Database seeding |
| Setup |
|
examples/typescript/db.ts |
Connection with pooling |
SQL (PostgreSQL)
| File |
Description |
examples/sql/indexes.sql |
B-tree, partial, covering, GIN indexes |
examples/sql/partitioning.sql |
Range, list, hash partitioning |
examples/sql/rls.sql |
Row-level security policies |
examples/sql/jsonb.sql |
JSONB operators, functions, indexing |
examples/sql/pg18-features.sql |
UUIDv7, async I/O, RETURNING OLD/NEW |
Reference Documentation
Detailed explanations in references/:
- SCHEMA.md - Column types, constraints, patterns
- QUERIES.md - Operators, joins, aggregations
- RELATIONS.md - One-to-many, many-to-many, relational API
- MIGRATIONS.md - drizzle-kit workflows
- POSTGRES.md - PostgreSQL 18 features, RLS, partitioning
- PERFORMANCE.md - Indexing, optimization, pooling
- CHEATSHEET.md - Quick reference
Resources
1---2name: postgres-drizzle-23description: PostgreSQL and Drizzle ORM best practices. Use when writing database schemas, queries, migrations, or any database-related code. Triggers on mentions of PostgreSQL, Postgres, Drizzle, database, schema, tables, columns, indexes, queries, migrations, ORM, relations, joins, transactions, or SQL. Proactively apply when creating APIs, backends, or data models.4---5
6# PostgreSQL + Drizzle ORM Best Practices
7
8Type-safe database applications with PostgreSQL 18 and Drizzle ORM.
9
10## Directory Structure
11
12```
13src/db/
14├── schema/
15│ ├── index.ts # Re-export all tables
16│ ├── users.ts # User table + relations
17│ └── posts.ts # Post table + relations
18├── db.ts # Database connection
19└── migrate.ts # Migration runner
20drizzle/
21└── migrations/ # Generated SQL migrations
22drizzle.config.ts # drizzle-kit configuration
23```
24
25## Essential Commands
26
27```bash
28npx drizzle-kit generate # Generate migration from schema
29npx drizzle-kit migrate # Apply migrations
30npx drizzle-kit push # Push schema directly (dev only)
31npx drizzle-kit studio # Database browser
32```
33
34## PostgreSQL 18 Highlights
35
36| Feature | Benefit |
37|---------|---------|
38| **UUIDv7** | Timestamp-ordered UUIDs, better index performance |
39| **Async I/O** | Up to 3x faster sequential scans |
40| **Index Skip Scan** | ~40% faster queries on composite indexes |
41| **RETURNING OLD/NEW** | Access previous values in UPDATE/DELETE |
42
43## Performance Checklist
44
45- [ ] Use `uuidv7()` for primary keys (PG18+) or `defaultRandom()`
46- [ ] Create indexes on foreign keys
47- [ ] Use partial indexes for filtered subsets
48- [ ] Use relational queries API to avoid N+1
49- [ ] Configure connection pooling in production
50- [ ] Run `EXPLAIN (ANALYZE, BUFFERS)` for slow queries
51
52---
53
54## Code Examples
55
56Complete, runnable examples in the `examples/` directory:
57
58### TypeScript (Drizzle ORM)
59
60| File | Description |
61|------|-------------|
62| **Schema** | |
63| `examples/typescript/schema/users.ts` | User table with timestamps, soft delete |
64| `examples/typescript/schema/posts.ts` | Posts with foreign key, indexes |
65| `examples/typescript/schema/many-to-many.ts` | Junction table pattern |
66| `examples/typescript/schema/enums-jsonb.ts` | Enums and typed JSONB |
67| **Queries** | |
68| `examples/typescript/queries/select.ts` | Filters, pagination, search |
69| `examples/typescript/queries/joins.ts` | Left/inner/multiple joins |
70| `examples/typescript/queries/aggregations.ts` | Count, sum, group by |
71| `examples/typescript/queries/mutations.ts` | Insert, update, delete, upsert |
72| `examples/typescript/queries/transactions.ts` | Transactions, savepoints |
73| **Relations** | |
74| `examples/typescript/relations/relational-queries.ts` | Nested queries, column selection |
75| **Migrations** | |
76| `examples/typescript/migrations/drizzle.config.ts` | drizzle-kit configuration |
77| `examples/typescript/migrations/migrate.ts` | Programmatic migration runner |
78| `examples/typescript/migrations/seed.ts` | Database seeding |
79| **Setup** | |
80| `examples/typescript/db.ts` | Connection with pooling |
81
82### SQL (PostgreSQL)
83
84| File | Description |
85|------|-------------|
86| `examples/sql/indexes.sql` | B-tree, partial, covering, GIN indexes |
87| `examples/sql/partitioning.sql` | Range, list, hash partitioning |
88| `examples/sql/rls.sql` | Row-level security policies |
89| `examples/sql/jsonb.sql` | JSONB operators, functions, indexing |
90| `examples/sql/pg18-features.sql` | UUIDv7, async I/O, RETURNING OLD/NEW |
91
92---
93
94## Reference Documentation
95
96Detailed explanations in `references/`:
97
98- **[SCHEMA.md](references/SCHEMA.md)** - Column types, constraints, patterns
99- **[QUERIES.md](references/QUERIES.md)** - Operators, joins, aggregations
100- **[RELATIONS.md](references/RELATIONS.md)** - One-to-many, many-to-many, relational API
101- **[MIGRATIONS.md](references/MIGRATIONS.md)** - drizzle-kit workflows
102- **[POSTGRES.md](references/POSTGRES.md)** - PostgreSQL 18 features, RLS, partitioning
103- **[PERFORMANCE.md](references/PERFORMANCE.md)** - Indexing, optimization, pooling
104- **[CHEATSHEET.md](references/CHEATSHEET.md)** - Quick reference
105
106---
107
108## Resources
109
110- **Drizzle Docs**: https://orm.drizzle.team
111- **PostgreSQL Docs**: https://www.postgresql.org/docs/18/
112- **Drizzle Studio**: `npx drizzle-kit studio`