Auth & Data
Model a schema with Drizzle, generate and apply migrations against Neon Postgres, and keep local and production in sync without data loss.
1 file
Description
Model a schema with Drizzle, generate and apply migrations against Neon Postgres, and keep local and production in sync without data loss.
Adding or altering a table, wiring the first DB connection, or resolving a migration that will not apply.
Define the schema in db/schema.ts:
import { pgTable, text, timestamp, boolean } from "drizzle-orm/pg-core";
export const users = pgTable("users", {
id: text("id").primaryKey(),
email: text("email").notNull().unique(),
isOwner: boolean("is_owner").default(false).notNull(),
createdAt: timestamp("created_at").defaultNow().notNull(),
});
Point drizzle.config.ts at the schema and set DATABASE_URL to the Neon pooled connection string.
Generate SQL from the schema diff: pnpm drizzle-kit generate. Read the generated file in drizzle/ before applying it.
Apply it: pnpm drizzle-kit migrate.
Inspect data with pnpm drizzle-kit studio.
DATABASE_URL in .env.local for dev and in the host env for production. Never hardcode it.generate locally and commit the SQL. Run migrate in a deploy step or manually against production. Do not push to production - push skips the migration history and can drop columns.drizzle-kit push is for fast local iteration only. It diffs and applies directly, which can silently drop a column when you rename one. Use generate + migrate anywhere that holds real data.DATABASE_URL. Guard the client creation so a missing env var does not fail the whole build.Added 2026-07-01. Back to the Skill Library.

New tutorials, open-source projects, and deep dives on coding agents - delivered weekly.