Setup and configuration guide for Drizzle ORM with schema management and migrations. WHEN: "set up Drizzle", "configure database schema", "run migrations", "drizzle-kit", "database connection", "seed database".
Golden-path setup for Drizzle ORM: installation, configuration, schema management, and migrations.
Install the core ORM and the Kit CLI:
npm install drizzle-orm
npm install -D drizzle-kit
Install the driver (Standard Postgres recommendation):
npm install postgres
(For Neon, use @neondatabase/serverless)
drizzle.config.ts)Use defineConfig for type safety. See the config template.
Key settings:
./src/db/schema.ts or ./src/db/schema/index.ts)../drizzle).Golden Path: Use a Modular Schema structure.
src/db/schema/ directory.users.ts, posts.ts).src/db/schema/index.ts.// src/db/schema/users.ts
import { pgTable, serial, text } from 'drizzle-orm/pg-core';
export const users = pgTable('users', {
id: serial('id').primaryKey(),
name: text('name'),
});
db.ts)Instantiate the client and ORM instance. See the connection template.
import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from 'postgres';
import * as schema from './schema';
const client = postgres(process.env.DATABASE_URL!);
export const db = drizzle(client, { schema });
Development (Rapid Prototyping):
Use push to sync schema directly to the DB without generating files.
npx drizzle-kit push
Production (Version Control):
Use generate to create SQL migration files, then apply them.
npx drizzle-kit generate
npx drizzle-kit migrate
Use drizzle-seed or a custom script.
npx tsx src/db/seed.ts
import { relations } from 'drizzle-orm';
import { users, posts } from './schema';
export const usersRelations = relations(users, ({ many }) => ({
posts: many(posts),
}));