Choosing a database
| Provider | Model | Best fit |
|---|---|---|
| Neon | Serverless Postgres, branching, scale-to-zero | New projects wanting plain Postgres + git-style database branches. |
| Supabase | Postgres + built-in Auth, Storage, Realtime | Wanting a full backend platform, not just a database. |
| Self-hosted PostgreSQL | You manage the server | Full control, compliance requirements, existing infrastructure. |
Connection patterns
Serverless functions open and close a lot of short-lived connections. A pooler sits in front of Postgres so you don't exhaust the database's connection limit.
Next.js function
Server Component / Route Handler
Connection pooler
PgBouncer-style, reuses connections
Postgres
Neon or Supabase
Tip
Use the pooled connection string for regular app queries, and the direct connection string only for migrations — most providers issue both.
Querying
Drizzle ORM gives you typed queries that compile to SQL you can still read. Parameterized raw SQL is the fallback for anything Drizzle doesn't express well.
Drizzle
const activeOrders = await db
.select()
.from(orders)
.where(and(eq(orders.userId, userId), eq(orders.status, 'active')))
.orderBy(desc(orders.createdAt))Parameterized raw SQL
const { rows } = await sql`
SELECT * FROM orders
WHERE user_id = ${userId} AND status = 'active'
`Watch out
Never build a query with string concatenation or template literals of raw user input — that's a SQL injection vector. Always use parameter placeholders, whether through an ORM or a tagged SQL template.
Schema migrations
| Tool | Does |
|---|---|
drizzle-kit generate | Diffs your schema file against the last migration and writes a new SQL migration. |
drizzle-kit migrate | Applies pending migrations to the target database. |
Supabase CLI (db diff) | Generates a migration from local schema changes. |
| Neon branching | Create a database branch to test a migration before applying it to production. |
Common column types
| Postgres type | Use for |
|---|---|
uuid | Primary keys that shouldn't be guessable or reveal row count. |
text | Variable-length strings — Postgres doesn't need a length limit like varchar(n). |
timestamptz | Timestamps — always store with time zone to avoid ambiguity. |
jsonb | Semi-structured data you need to query into — indexable, unlike json. |
numeric | Money and exact decimals — never float/double for currency. |
Environment variables
| Variable | Holds |
|---|---|
DATABASE_URL | Pooled connection string, used by the running app. |
DATABASE_URL_UNPOOLED | Direct connection string, used for migrations. |
NEXT_PUBLIC_SUPABASE_URL | Supabase project URL — safe to expose to the browser. |
NEXT_PUBLIC_SUPABASE_ANON_KEY | Public key, relies on Row Level Security to stay safe client-side. |
Note
Connecting a database integration in this project injects these variables automatically — you shouldn't need to copy connection strings by hand.
