devtools logo
by Ryan Macalandag
Back to cheatsheets
Cheatsheetsreactnextjsdatabasesupabaseneonpostgresql

React / Next.js Databases Cheatsheet

Database patterns with Supabase, Neon, and PostgreSQL.

Choosing a database

ProviderModelBest fit
NeonServerless Postgres, branching, scale-to-zeroNew projects wanting plain Postgres + git-style database branches.
SupabasePostgres + built-in Auth, Storage, RealtimeWanting a full backend platform, not just a database.
Self-hosted PostgreSQLYou manage the serverFull 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

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'
`

Schema migrations

ToolDoes
drizzle-kit generateDiffs your schema file against the last migration and writes a new SQL migration.
drizzle-kit migrateApplies pending migrations to the target database.
Supabase CLI (db diff)Generates a migration from local schema changes.
Neon branchingCreate a database branch to test a migration before applying it to production.

Common column types

Postgres typeUse for
uuidPrimary keys that shouldn't be guessable or reveal row count.
textVariable-length strings — Postgres doesn't need a length limit like varchar(n).
timestamptzTimestamps — always store with time zone to avoid ambiguity.
jsonbSemi-structured data you need to query into — indexable, unlike json.
numericMoney and exact decimals — never float/double for currency.

Environment variables

VariableHolds
DATABASE_URLPooled connection string, used by the running app.
DATABASE_URL_UNPOOLEDDirect connection string, used for migrations.
NEXT_PUBLIC_SUPABASE_URLSupabase project URL — safe to expose to the browser.
NEXT_PUBLIC_SUPABASE_ANON_KEYPublic key, relies on Row Level Security to stay safe client-side.

Spot an error or have a suggestion? Open an issue on GitHub.