Drizzle ORM Setup
Configure Drizzle ORM with your Supabase Postgres database connection.
Last reviewed: ยท Documentation review; fresh installation testing pending.
Prerequisites
Supabase environment variables configured
Your .env.local file should contain the Postgres connection string from Supabase.
Verify with
grep -E '^(POSTGRES_URI|POSTGRES_MIGRATION_URI)=' .env.local | cut -d= -f1Expected: POSTGRES_URI and POSTGRES_MIGRATION_URI (names only)
Follow the Supabase Setup guide to configure your environment variables, including both runtime and migration connection strings.
Supabase Setup GuideFor best results, run this prompt in plan mode.
Prompt
Set up Drizzle ORM for this project with Supabase Postgres.
Requirements:
1. Install drizzle-orm and postgres as dependencies and drizzle-kit and @next/env as dev dependencies
2. Create a drizzle.config.ts file in the project root configured to:
- Load .env.local with loadEnvConfig from @next/env
- Use POSTGRES_MIGRATION_URI (Session pooler, port 5432) for migrations and Studio; fail clearly if it is missing
- Output migrations to a drizzle folder
- Use the src/db/schema.ts file for the schema
3. Create a server-only src/db/index.ts exporting a module-scoped drizzle client using POSTGRES_URI (Transaction pooler, port 6543). Configure postgres-js with prepare: false, max: 1, and ssl: "require". Fail clearly if the variable is missing.
4. Create src/db/schema.ts with an empty schema file that's ready for table definitions
5. Add the following scripts to package.json:
- "db:generate": "drizzle-kit generate"
- "db:migrate": "drizzle-kit migrate"
- "db:studio": "drizzle-kit studio"
6. Create CLAUDE.md in the project root if it doesn't exist, and add a "Database" section containing concise instructions:
- Commands for generating and applying migrations (db:generate, db:migrate)
- Always enable Row Level Security (RLS) and include policies when creating exposed tables
- The postgres database role bypasses RLS: authenticate and authorize every server-side operation; do not assume a direct Drizzle connection enforces user policies
- Prefer configuration via custom SQL migrations when possible (e.g., Supabase storage buckets, database functions, triggers)
Use the postgres-js driver (drizzle-orm/postgres-js with the postgres package). Make sure to handle the SSL connection properly for Supabase.