Netlify
Netlify Database is a platform that applies the migrations itself, and sets its own variable rather than DATABASE_URL. This documentation site runs on it, so it is the worked example: the PGlite in dev, Postgres in prod setup, with the socket's netlify preset standing in for Netlify Database in development, plus an in-browser instance for the examples:
export default defineNuxtConfig({
modules: ['nuxt-pglite'],
pglite: {
client: { enabled: true }, // the interactive examples
server: {
enabled: false, // nothing of PGlite on the server
},
socket: { port: 5456, provider: 'netlify' }, // `NETLIFY_DB_URL` while `nuxt dev` runs
},
})
How it fits together
Netlify Database is a Postgres that Netlify provisions for the site. It sets NETLIFY_DB_URL in builds and functions (never DATABASE_URL), and applies the SQL files in netlify/database/migrations itself, right before it publishes a deploy. Each deploy preview gets its own database branch, with the migrations of that deploy applied to it.
nuxt dev | Deployed on Netlify | |
|---|---|---|
NETLIFY_DB_URL | the socket's URL, from the netlify preset | the site's database (or the preview's branch) |
NETLIFY_DB_DRIVER | server: pg over the wire protocol | serverless, set by Netlify in its functions: Neon's HTTP driver |
netlify/database/migrations | applied by init with applyMigrations | applied by Netlify before publishing |
| Applied migrations recorded in | netlify.migrations, applyMigrations's default | netlify.migrations |
The routes only know drizzle-orm/netlify-db over the connection @netlify/database opens. Nothing else changes between development and production.
Try it
The demo below calls /api/visits, a route that uses Drizzle over NETLIFY_DB_URL. In nuxt dev the socket exports that variable and the route reaches PGlite; on the deployed site Netlify sets it, and the route tells you when the database is not enabled.
What ends up in the build
| Server output | Client output | |
|---|---|---|
| PGlite engine and WebAssembly | absent (server.enabled: false) | present, loaded in a worker on first use |
server/pglite.config.ts | absent: only the socket reads it, in nuxt dev | absent |
app/pglite.config.ts | absent | present |
@netlify/database and your driver (drizzle-orm/netlify-db, pg) | present | absent |
Steps
Write the schema and generate migrations
import { pgTable, serial, timestamp } from 'drizzle-orm/pg-core'
export const visits = pgTable('visits', {
id: serial('id').primaryKey(),
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
})
import { defineRelations } from 'drizzle-orm'
import * as schema from './schema'
export const relations = defineRelations(schema)
drizzle-kit 1.0.0-rc.4 or later writes its migrations where Netlify looks for them:
import { defineConfig } from 'drizzle-kit'
// Netlify applies `out` on deploy and `init` applies it locally: no `push`/`migrate`.
export default defineConfig({
out: './netlify/database/migrations',
schema: './server/database/schema.ts',
dialect: 'postgresql',
dbCredentials: {
// the development socket, for `studio` and `check` while `nuxt dev` runs
url: 'postgres://postgres@127.0.0.1:5456/postgres',
},
})
npx drizzle-kit generate --name init
Commit the generated netlify/database/migrations/<timestamp>_init/ directory: the deploy is what applies it.
drizzle-kit push or drizzle-kit migrate against the site's database. Netlify records the files it applied in netlify.migrations; a schema pushed or migrated by another tool is one it does not know about, and the next deploy's migrations run against it.Apply them in development
The socket's instance applies the same files from init, with the same bookkeeping as Netlify:
import { fileURLToPath } from 'node:url'
import { applyMigrations } from 'nuxt-pglite/migrations'
const migrations = fileURLToPath(new URL('../netlify/database/migrations', import.meta.url))
export default definePGliteServerConfig({
init: (pg) => applyMigrations(pg, migrations),
})
Query through @netlify/database and drizzle-orm/netlify-db
getDatabase() from @netlify/database reads NETLIFY_DB_URL and picks the driver from NETLIFY_DB_DRIVER: Neon's serverless driver under serverless, which Netlify sets in its functions, pg over the wire protocol otherwise, which is what the socket exports as server, since it cannot answer Neon's drivers. Drizzle takes that connection as its client:
import type { DatabaseConnection, ServerlessDatabaseConnection } from '@netlify/database'
import { MissingDatabaseConnectionError, getDatabase } from '@netlify/database'
import { drizzle } from 'drizzle-orm/netlify-db'
import { relations } from '../database/relations'
type HttpClient = ServerlessDatabaseConnection['httpClient']
/** Routes Drizzle's `http(text, params, options)` calls to `http.query()`. */
function queryable(http: HttpClient): HttpClient {
type Rest = Parameters<HttpClient['query']> extends [string, ...infer R] ? R : never
const call = (text: TemplateStringsArray | string, ...rest: Rest) =>
typeof text === 'string' ? http.query(text, ...rest) : http(text, ...rest)
return Object.assign(call, {
query: http.query.bind(http),
transaction: http.transaction,
unsafe: http.unsafe.bind(http),
})
}
function createDB() {
const client: DatabaseConnection = getDatabase()
return drizzle({
client:
client.driver === 'serverless'
? { ...client, httpClient: queryable(client.httpClient) }
: client,
relations,
})
}
let db: ReturnType<typeof createDB> | undefined
/** `503` while `NETLIFY_DB_URL` is unset, so a client can tell it from a bug. */
export function useDB() {
try {
db ??= createDB()
} catch (error) {
if (error instanceof MissingDatabaseConnectionError) {
throw createError({
statusCode: 503,
statusMessage: 'No database configured',
message: '`NETLIFY_DB_URL` is unset, so Netlify Database is not enabled on this site.',
data: { reason: 'unconfigured' },
})
}
throw error
}
return db
}
getDatabase() throws MissingDatabaseConnectionError while NETLIFY_DB_URL is unset, which useDB turns into a 503 with a reason in the error's data: every route that needs the database answers the same way, and a client can tell a missing database from a failing one.
queryable works around drizzle-orm1.0.0-rc.5, whose netlify-db adapter calls Neon's HTTP client as http(text, params, options), a form @neondatabase/serverless 1.x only accepts through http.query(): without it, every query on a deployed site fails with "This function can now be called only as a tagged-template function". nuxt dev never hits it, since the socket's pg path does not go through the HTTP client. Drop the wrapper once Drizzle routes the call itself.Add a netlify.toml
Netlify detects Nitro, which picks its netlify preset by itself. In a monorepo, point the build at the app's directory:
# Netlify applies `netlify/database/migrations` itself before publishing: no migration command.
[build]
base = "docs"
command = "pnpm build"
publish = "dist"
[build.environment]
NODE_VERSION = "24"
publish is relative to base: the netlify preset writes the static files to dist and the server to .netlify/functions-internal. For an app at the root of its repository, drop base.
Enable Netlify Database on the site
Netlify provisions the database on the first deploy of a site that depends on @netlify/database, as this one does; otherwise, enable it from the site's Data & Storage → Database page in the Netlify UI. Once enabled, NETLIFY_DB_URL is set for builds and functions with nothing to configure.
Deploy
Push to the branch Netlify builds, or run netlify deploy --build. Netlify applies the pending migrations before publishing, and a failing one blocks the publish.
Without the database enabled, the site still deploys and every page works; the routes that need the database answer 503 with a message naming NETLIFY_DB_URL. The demo above shows which case this deployment is in.
applyMigrations does the same, so the mistake shows up in nuxt dev rather than in a failed deploy.