Netlify DB

Guide for using Netlify DB (managed Neon Postgres). Use when the project needs a relational database, structured data storage, SQL queries, or data

What Is This?

Overview

Netlify DB is a managed relational database service built on top of Neon Postgres, integrated directly into the Netlify platform. When you enable Netlify DB for a project, Netlify automatically provisions a Neon Postgres database without requiring a separate Neon account or any manual database setup. The service handles infrastructure, connection pooling, and environment configuration so developers can focus on writing queries and building features.

The integration provides two primary ways to interact with the database: raw SQL through the @netlify/neon package and a structured query approach using Drizzle ORM. Both methods connect to the same underlying Postgres instance, giving teams flexibility to choose the abstraction level that fits their workflow. Netlify DB also supports deploy preview branching, which creates isolated database branches for each pull request preview, keeping development and production data separate.

Netlify DB is designed for projects that need structured, relational data storage with SQL capabilities. It is not a key-value store or a document database. For unstructured file storage or binary data, Netlify Blobs is the more appropriate choice.

Who Should Use This

  • Full-stack developers building Netlify-hosted applications that require persistent relational data
  • Teams migrating from a self-managed Postgres instance who want reduced operational overhead
  • Developers using Drizzle ORM who want a managed Postgres backend with zero provisioning steps
  • Engineers who need isolated database environments per pull request for safe feature development
  • Developers building applications with structured data models, foreign key relationships, or complex SQL queries
  • Startups and small teams that want production-grade Postgres without managing a separate database service

Why Use Netlify DB?

Problems It Solves

  • Eliminates the need to create and configure a separate database account or service for Postgres hosting
  • Removes manual environment variable setup by automatically injecting the connection string into the Netlify build and function environment
  • Prevents data contamination between preview deployments and production by providing branch-level database isolation
  • Reduces the complexity of schema migrations by integrating with Drizzle Kit for version-controlled migration workflows
  • Avoids the overhead of connection management by using Neon's serverless driver, which is optimized for short-lived function invocations

Core Highlights

  • Automatic Neon Postgres provisioning with no external account required
  • Raw SQL support via the @netlify/neon package using the Neon serverless driver
  • Drizzle ORM integration for type-safe, schema-driven database access
  • Deploy preview branching creates a separate database branch per preview environment
  • Environment variables are injected automatically into Netlify Functions and edge functions
  • Schema migrations managed through Drizzle Kit with a straightforward CLI workflow
  • Compatible with standard Postgres tooling and SQL syntax
  • Clear guidance on when to use Netlify Blobs instead for non-relational storage needs

How to Use Netlify DB?

Basic Usage

After enabling Netlify DB in your project, install the required package and start querying using raw SQL.

npm install @netlify/neon
import { neon } from "@netlify/neon";

const sql = neon(process.env.DATABASE_URL);

export default async function handler(req, context) {
  const results = await sql`SELECT * FROM users WHERE active = true`;
  return Response.json(results);
}

Specific Scenarios

Using Drizzle ORM for type-safe queries:

npm install drizzle-orm @netlify/neon
npm install -D drizzle-kit
import { drizzle } from "drizzle-orm/neon-http";
import { neon } from "@netlify/neon";
import { users } from "./schema";

const sql = neon(process.env.DATABASE_URL);
const db = drizzle(sql);

const activeUsers = await db.select().from(users).where(eq(users.active, true));

Running migrations with Drizzle Kit:

npx drizzle-kit generate
npx drizzle-kit migrate

Real-World Examples

  • An e-commerce application storing product inventory, orders, and customer records with relational integrity enforced through foreign keys
  • A content management system where authors, posts, and categories are linked through normalized tables and queried with joins
  • A SaaS application using deploy preview branches to test schema changes against isolated data before merging to production

When to Use Netlify DB?

Use Cases

  • Storing user accounts, sessions, and profile data for authenticated applications
  • Managing product catalogs, pricing tables, and inventory with relational constraints
  • Building analytics dashboards that aggregate structured event data using SQL
  • Persisting form submissions that require querying, filtering, and reporting
  • Handling multi-tenant data with row-level isolation enforced through SQL conditions
  • Supporting applications where data volume grows over time and requires indexed queries
  • Replacing a spreadsheet or flat-file data store with a queryable relational structure