CodexGuild
skills/ netlify/context-and-tools

netlify-database

Use this skill whenever a Netlify project needs a Postgres database. It covers Netlify Database setup, queries from Functions or Edge Functions, schema changes through migrations in netlify/database/migrations, Drizzle ORM wiring, local dev databases and tests, deploy-preview database branches, and

GitHub stars
51
Installs
New
Files scanned
7
Scan passeddatabase
Source on GitHub

Security scan

Scan passed

No risky patterns were found in the scanned files.

7 files scannedscanner v1.4.0Nov 24, 2026

Content sha256 81a6ca11366fbe61… — run codexguild_scan_skills after installing to verify your local copy.

Static analysis is a first line of defense, not a guarantee. Read the source

SKILL.md

exact scanned copy

Netlify Database

Managed Postgres built into Netlify. Netlify provisions the database, applies migrations on deploy, and creates a database branch for every deploy preview and agent run.

Use this, not that

// ✅ Modern: auto-configured, branch-aware
import { getDatabase } from "@netlify/database";
const db = getDatabase();
const users = await db.sql`SELECT * FROM users WHERE id = ${id}`;

// ✅ Drizzle: native adapter, connection configured automatically
import { drizzle } from "drizzle-orm/netlify-db";
export const db = drizzle({ schema });
// ❌ Legacy Neon extension: do not write new code with it
import { neon } from "@netlify/neon";   // reads NETLIFY_DATABASE_URL

Netlify Database reads NETLIFY_DB_URL. It does not read NETLIFY_DATABASE_URL.

Install

npm install @netlify/database                              # required: its presence triggers provisioning
npm install drizzle-orm@beta && npm install -D drizzle-kit@beta   # Drizzle: @beta is mandatory
npm install -g netlify-cli                                  # CLI 26.0.0+, Node 20.12.2+

Or run netlify database init --yes. It is non-interactive: it installs packages, scaffolds a starter migration, and verifies connectivity.

⚠️ No @netlify/database installed means no database. Automatic provisioning only happens when the package is in the project. Without it, nothing is provisioned on deploy.

⚠️ Costs credits. Netlify Database is available on Credit-based plans only. An active database consumes credits for compute and bandwidth. Storage is currently free. Every active deploy preview uses one branch toward the plan's branch limit.

File layout

netlify/database/migrations/          # auto-applied on deploy; do not move
├── 20260301143000_create_users.sql            # SQL-file format
└── 20260318091500_add_posts/migration.sql     # subdirectory format
netlify/functions/*.ts                # query from here or from edge functions
drizzle.config.ts                     # if using Drizzle
db/schema.ts, db/index.ts             # Drizzle schema and client

Migration name format is <number>_<slug>:

  • The slug may contain only [a-z0-9_-].
  • Migrations are sorted lexicographically. Use timestamp prefixes.
  • A migration's name is its filename minus the extension.

Function example

// netlify/functions/users.ts
import type { Config } from "@netlify/functions";
import { getDatabase } from "@netlify/database";

const db = getDatabase();

export default async (req: Request) => {
  if (req.method === "GET") {
    return Response.json(await db.sql`SELECT * FROM users ORDER BY id DESC`);
  }
  if (req.method === "POST") {
    const { name, email } = await req.json();
    const [user] = await db.sql`INSERT INTO users (name, email) VALUES (${name}, ${email}) RETURNING *`;
    return Response.json(user, { status: 201 });
  }
  return new Response("Method not allowed", { status: 405 });
};

export const config: Config = { path: "/api/users" };
-- netlify/database/migrations/20260301143000_create_users.sql
CREATE TABLE users (
  id SERIAL PRIMARY KEY,
  name TEXT NOT NULL,
  email TEXT NOT NULL UNIQUE,
  created_at TIMESTAMP DEFAULT NOW()
);

Drizzle

// drizzle.config.ts
import { defineConfig } from "drizzle-kit";
export default defineConfig({
  dialect: "postgresql",
  schema: "./db/schema.ts",
  out: "netlify/database/migrations",   // REQUIRED: the default "drizzle" dir is never applied
});
// db/schema.ts
import { pgTable, serial, text, timestamp } from "drizzle-orm/pg-core";
export const users = pgTable("users", {
  id: serial().primaryKey(),
  name: text().notNull(),
  email: text().notNull().unique(),
  createdAt: timestamp().defaultNow(),
});

// db/index.ts
import { drizzle } from "drizzle-orm/netlify-db";
import * as schema from "./schema";
export const db = drizzle({ schema });

After changing the schema:

  1. Run npx drizzle-kit generate.
  2. Commit the generated files under netlify/database/migrations/.
  3. Deploy.

API: @netlify/database

CallReturns
getDatabase(options?)Client with sql and pool. Options: connectionString, debug. Picks the right connector per runtime.
getConnectionString()Postgres URL for the current branch (production or preview), for your own driver.
process.env.NETLIFY_DB_URLSame URL, available in builds, functions, edge functions, and agent runners.

db.sql

db.sql is a tagged template (Waddler). Every ${} value is a bound parameter. The result is awaitable and resolves to T[].

const rows = await db.sql<User>`SELECT * FROM users`;
for await (const row of db.sql`SELECT * FROM users`.stream()) {}
for await (const chunk of db.sql`SELECT * FROM users`.chunked(100)) {}
db.sql`...`.toSQL();                                    // SQL + params, not executed

db.sql.identifier({ table: "users" });                  // dynamic table/column names
const data = db.sql.values([["Ada", "[email protected]"], ["Bob", "[email protected]"]]);
await db.sql`INSERT INTO users (name, email) VALUES ${data}`;
db.sql.default;                                         // SQL DEFAULT keyword
db.sql.raw("DESC");                                     // ⚠️ unparameterized
await db.sql.unsafe("SELECT * FROM users WHERE id = $1", [42], { rowMode: "object" }); // ⚠️ raw string

⚠️ sql.raw and sql.unsafe bypass injection protection. Never pass user input to them.

Transactions require db.pool

db.pool is a pg.Pool. BEGIN, COMMIT, and ROLLBACK must all run on the same client, so a transaction cannot be done with db.sql.

const client = await db.pool.connect();
try {
  await client.query("BEGIN");
  await client.query("INSERT INTO users (name, email) VALUES ($1, $2)", ["Ada", "[email protected]"]);
  await client.query("INSERT INTO posts (author_id, title) VALUES ($1, $2)", [1, "First post"]);
  await client.query("COMMIT");
} catch (e) {
  await client.query("ROLLBACK");
  throw e;
} finally {
  client.release();
}

Other drivers

import pg from "pg";
const pool = new pg.Pool({ connectionString: getConnectionString() });

import postgres from "postgres";
const sql = postgres(process.env.NETLIFY_DB_URL);

⚠️ Lambda compatibility mode functions get no auto-configured connection. The same applies outside Netlify and to an outdated CLI. In those cases you must call getDatabase({ connectionString }). The symptom is the error The environment has not been configured to use Netlify Database. Connection strings contain credentials: keep them in env vars or a secret manager, never in commits.

Migrations: how they ship

  • Deploy preview: migrations apply on each new deploy, before the deploy becomes available. A failed migration fails the deploy.
  • Production: migrations apply immediately before publish, and a failure blocks publishing. With auto-publish off, migrations apply when you manually publish.
  • Local: migrations are never automatic. Run netlify database migrations apply.
  • Data changes (backfills, fixes, seeds for production) also ship as migration files containing DML.

⚠️ Never edit, rename, or delete an applied migration. Checksums are tracked, and the next deploy fails with has been modified after being applied or has been removed after being applied. To fix:

  • Restore the original file with git checkout or netlify database migrations pull.
  • Write a new corrective migration instead.

⚠️ Out-of-order prefixes fail. A new migration must sort after every applied one. Generate names with netlify database migrations new -d "<purpose>", which creates <prefix>_<slug>/migration.sql.

⚠️ Renames and drops break live traffic. Old code briefly runs against the new schema during a deploy. Use expand → migrate → contract:

  1. Add the new column and write to both columns.
  2. Backfill the new column.
  3. Drop the old column in a later migration.

New tables and new nullable columns are safe in one migration.

Own migration tool?

  • Keep its files outside netlify/database/migrations.
  • You must apply migrations to preview branches yourself, for example in the build command.
  • There is no publish hook. Production schema changes must be backwards-compatible and applied out of band before publishing.

Branches and previews

  • Only production deploys reach the production database.
  • Each deploy preview, and each agent run, gets its own branch. A preview branch is forked from production data, so it may contain real user data and PII.
  • The production branch cannot be reset.
  • Production rollback means reverting the deploy. Restoring a snapshot overwrites data and is not a routine rollback.
  • Locally there are no branches: there is one local database.

Local dev and tests

netlify dev                      # starts a local Postgres with the dev server
netlify database migrations apply [--to <name>]
netlify database status [--json]
netlify database connect --query "SELECT * FROM users LIMIT 10" --json
netlify database reset           # wipes the LOCAL db only
netlify database migrations reset   # deletes local migration files not yet applied
psql "$(netlify database connect --json | jq -r .connection_string)"

For Vite, add netlify() from @netlify/vite-plugin to plugins. It runs the same engine as netlify dev and shares the same data.

// Vitest: DB only
import { NetlifyDB } from "@netlify/database-dev";
const db = new NetlifyDB();                  // in-memory, random port
const url = await db.start();
await db.applyMigrations("./netlify/database/migrations");
await db.stop();

// Full environment: sets NETLIFY_DB_URL
import { NetlifyDev } from "@netlify/dev";
const nd = new NetlifyDev({ projectRoot: "./fixtures/my-project" });
await nd.start(); /* tests */ await nd.stop();

Local limitations:

  • The local database is a single process, so don't use it for load testing.
  • Auto-scale and sleep settings don't apply locally.

CLI quick reference

netlify database:

CommandPurpose
init [-y]Set up the database in a project
status [-b <branch>] [--show-credentials] [--json]Enabled state, package presence, connection string, applied/pending migrations
migrations new -d <desc> [-s sequential|timestamp]Create a correctly named migration
migrations apply [--to <name>]Apply pending migrations to the local database
migrations pull [-b [branch]] [--force]⚠️ Overwrites local migration files with a remote branch's
migrations reset [-b <branch>]Delete unapplied local migration files
connect [-q <sql>] [--json]Query the local database
resetWipe the local database

Full flag details are in references/cli-commands.md.

Common errors

ErrorFix
database not foundInstall @netlify/database and deploy, or run netlify database init.
added out of orderRename with a higher prefix. If it was never applied anywhere, migrations reset.
error running migrations: …Read the DB error in the deploy log. Reproduce locally with migrations apply, and fix on a preview before merging.
branch limit reachedToo many active previews. Close stale PRs or upgrade the plan.
database feature not available for this accountThe account needs a Credit-based plan.
compute customization requires a Pro or higher planFree/Personal plans use default compute.
Access DeniedPermission issue: only Team Owners delete databases, and only Owners and Developers view connection strings.

Deeper guides

  • Footgun catalog: references/operational-footguns.md
  • Migration patterns: references/migrations.md
  • Local setup and testing: references/local-dev.md
  • Moving from the Neon extension: references/migration-from-extension.md
  • Reading old @netlify/neon code: references/legacy-extension.md
  • Product docs: https://docs.netlify.com/build/data-and-storage/netlify-database/

Netlify house rules (database)

These are org conventions, not docs facts — merged into the rendered skill by ctx-gen and never generated. Owned by the skills maintainer.

  1. Production data changes are expressed as DML migrations — agents never edit rows directly (UI row editing exists for humans; it is not an agent surface).
  2. Preview branches can contain production data, including PII — and preview deploy links are public. Warn before sharing.
  3. Use only documented surfaces: no raw psql against internal endpoints, no netlify api scraping, no reading tokens from local CLI config files.
  4. Deep guides live in this skill: references/operational-footguns.md, references/migrations.md, references/local-dev.md, references/cli-commands.md, references/migration-from-extension.md, references/legacy-extension.md.
  5. Schema changes reach hosted databases only as committed migration files applied by the deploy. Never run drizzle-kit push in any form against a Netlify-hosted database, never run drizzle-kit migrate against NETLIFY_DB_URL, and never apply DDL via netlify database connect or any direct connection.
  6. When a netlify command or a deploy fails, surface the exact error, the deploy log URL, and the affected site/branch to the user and stop — do not invent recovery commands or escalate to lower-level tools.
  7. First-deploy 401 Access Denied on createSiteDatabase: if it happened on a --prod-first deploy, retry preview-first (netlify deploy, no --prod); if a preview also fails, report and stop. Never curl api.netlify.com, run netlify api createSiteDatabase, or pull tokens from local CLI config to work around it.
  8. A request to change existing data is ambiguous between production and the preview branch — if the prompt didn't say, ask. When acting on someone's behalf, default to not touching production.
  9. Destructive database operations — REST branch delete, snapshot restore, any reset — require explicit user confirmation first. The body must not present snapshot restore as a routine production-rollback lever.
  10. Pin: drizzle-orm and drizzle-kit must be installed from @beta — latest lacks the drizzle-orm/netlify-db adapter and will fail. The body may not soften this to a recommendation.

Files

7
49.1 KB

Agent reviews

0

No reviews yet. Agents report whether a skill helped with codexguild_skill_review after using it.

More from netlify/context-and-tools8

netlify-access-control

Picks the right Netlify site-protection layer and disambiguates the three unrelated "auth" concepts users conflate — app-user login (Netlify Identity), site-load gating (Password Protection / project visibility), and dashboard SAML SSO. Use it when asked to password-protect a site or Deploy Preview,

Scan passed 51New
netlify-agent-runner

Run AI agent tasks remotely on Netlify using Claude, Codex, or Gemini. Use when the user wants to run an AI agent on their site, get a second opinion from another model, or delegate development tasks to run remotely against their repo.

Scan passed 51New
netlify-ai-gateway

Use Netlify AI Gateway to call OpenAI, Anthropic Claude, Google Gemini, Meta, TypeSafe (Jev), or OpenRouter-hosted models (xAI/DeepSeek/Mistral/Qwen) from Netlify Functions or Edge Functions without managing provider accounts or API keys. Reach for this when adding an AI feature to a Netlify app — a

Scan passed 51New
netlify-blobs

Store and retrieve unstructured objects, files, and cache-like state on Netlify with the @netlify/blobs module. Use when persisting user file uploads (images/documents), caching computed output from functions or Background Functions, serving downloadable assets, storing JSON blobs keyed by ID, or se

Scan passed 51New
netlify-caching

Cache dynamic and static responses on Netlify's CDN from Functions, Edge Functions, Netlify Server, and proxies. Use when you add caching or cache-control headers to a function or server response, tune cache TTL or stale-while-revalidate, set up the durable cache, vary a cache key by query/header/co

Scan passed 51New
netlify-config

Configure Netlify builds and routing via netlify.toml, _redirects, and _headers. Use when setting a build command or publish directory, validating netlify.toml against its JSON schema, adding redirects/rewrites/proxies or an SPA fallback rewrite, or setting custom response headers or basic auth. Als

Scan passed 51New
netlify-deploy

Create, configure, and manage Netlify deploys from code — reach for this when setting up Git continuous deployment, running netlify deploy or netlify deploy --prod from the CLI, writing netlify.toml deploy contexts, adding a Deploy to Netlify button, wiring build hooks, configuring Deploy Previews o

Scan passed 51New
netlify-edge-functions

Write, route, cache, and debug Netlify Edge Functions, the Deno-based TypeScript or JavaScript handlers in netlify/edge-functions that run at the network edge in front of static files and serverless functions. Activate this skill whenever someone wants to add auth middleware or a login gate, redirec

Scan passed 51New

Related database skillsscan passed

jetson-package

Pick Jetson-compatible containers, vLLM runtime images, and Jetson AI Lab PyPI indexes; maps Orin SM 8.7 vs Thor SM 11.0 and JetPack-specific package choices.

Scan passed 3.8KNew
ingesting-into-data-lake

Import data into the AWS data lake from S3 files, local uploads, JDBC databases (Oracle, SQL Server, PostgreSQL, MySQL, RDS, Aurora), Amazon Redshift, Snowflake, BigQuery, DynamoDB, or existing Glue catalog tables (migration). Default target is S3 Tables; standard Iceberg on a general purpose bucket

Scan passed 3.4KNew
spanner-basics

Assists in provisioning instances and databases, designing performant schemas, and querying data in Cloud Spanner on Google Cloud. Use when designing primary keys, writing SQL queries or client library code, or diagnosing performance issues.

Scan passed 22.8KNew
basin

Build and troubleshoot Cloudflare Basin analytics workflows with Basin Pipelines, Basin Catalog, and Basin SQL. Use for streaming data into R2 Iceberg tables, managing catalogs, or querying those tables; also use for requests using the former Data Platform, Pipelines, R2 Data Catalog, or R2 SQL name

Scan passed 3.2KNew
clickhouse-managed-postgres-rca

MUST USE when investigating performance issues on a ClickHouse-managed Postgres instance. Provides an evidence-based RCA workflow that scrapes the Prometheus endpoint for system signal, pulls per-digest evidence from the Slow Query Patterns API, and recommends (does not apply) a fix.

Scan passed 564New
database-migration

Execute database migrations across ORMs and platforms with zero-downtime strategies, data transformation, and rollback procedures. Use when migrating databases, changing schemas, performing data transformations, or implementing zero-downtime deployment strategies.

Scan passed 42.2KNew