Neon

According to their official website, Neon is a fully managed serverless Postgres.

Drizzle ORM natively supports both the Neon serverless driver with the drizzle-orm/neon-serverless package and the postgres or pg drivers for accessing a Neon Postgres database.

npm
yarn
pnpm
bun
npm i drizzle-orm @neondatabase/serverless
npm i -D drizzle-kit

With the Neon serverless driver [github, blog post, Neon docs],
you can access a Neon database from serverless environments — like Cloudflare Workers or Vercel Edge Functions — over HTTP or WebSockets instead of TCP.

Querying over HTTP is faster for single, non-interactive transactions. If you require session or interactive transaction support or a fully-compatible drop-in replacement for the pg driver, use WebSockets.

HTTP
WebSockets
index.ts
import { neon } from '@neondatabase/serverless';
import { drizzle } from 'drizzle-orm/neon-http';

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

const result = await db.select().from(...);
index.ts
import { Pool } from '@neondatabase/serverless';
import { drizzle } from 'drizzle-orm/neon-serverless';

const pool = new Pool({ connectionString: env.DATABASE_URL });
const db = drizzle(pool)

const result = await db.select().from(...);

For an example of using Drizzle ORM with the Neon Serverless driver in a Cloudflare Worker, see here.

To use Neon from a serverfull environment, you can use the PostgresJS driver, as described in Neon’s official nodejs docs — see docs.

Postgres.JS

According to the official website, PostgresJS is the fastest fully featured PostgreSQL client for Node.js and Deno.

Drizzle ORM natively supports postgresjs driver with drizzle-orm/postgres-js package.

npm
yarn
pnpm
bun
npm i drizzle-orm postgres
npm i -D drizzle-kit
index.ts
import { drizzle } from 'drizzle-orm/postgres-js';
import { migrate } from 'drizzle-orm/postgres-js/migrator';
import postgres from 'postgres';

// for migrations
const migrationClient = postgres("postgres://postgres:[email protected]:5432/db", { max: 1 });
migrate(drizzle(migrationClient), ...)

// for query purposes
const queryClient = postgres("postgres://postgres:[email protected]:5432/db");
const db = drizzle(queryClient);
await db.select().from(...)...
⚙️

For the built in migrate function with DDL migrations we strongly encourage you to use max: 1 connection configuration.

For querying purposes feel free to use pool size of your choice based on your business demands.

node-postgres

According to the official website, node-postgres is a collection of Node.js modules for interfacing with your PostgreSQL database.

Drizzle ORM natively supports pg with drizzle-orm/pg package.

npm
yarn
pnpm
bun
npm i drizzle-orm pg
npm i -D drizzle-kit @types/pg

You can connect to a PostgreSQL database either using a single client connection or a pool.

Client connection
Pool connection
index.ts
import { pgTable, serial, text, varchar } from "drizzle-orm/pg-core";
import { drizzle } from "drizzle-orm/node-postgres";
import { Client } from "pg";

const client = new Client({
  connectionString: "postgres://user:password@host:port/db",
});

// or
const client = new Client({
  host: "127.0.0.1",
  port: 5432,
  user: "postgres",
  password: "password",
  database: "db_name",
});

await client.connect();
const db = drizzle(client);
index.ts
import { pgTable, serial, text, varchar } from "drizzle-orm/pg-core";
import { drizzle } from "drizzle-orm/node-postgres";
import { Pool } from "pg";

const pool = new Pool({
  connectionString: "postgres://user:password@host:port/db",
});

// or
const pool = new Pool({
  host: "127.0.0.1",
  port: 5432,
  user: "postgres",
  password: "password",
  database: "db_name",
});

const db = drizzle(pool);

Usage with Cloudflare Workers

Now that Cloudflare Workers supports TCP connections, you can use node-postgres to connect to connection poolers, e.g. pgBouncer.

worker.ts
import { Client } from "pg";
import { drizzle } from "drizzle-orm/node-postgres";

export default {
  async fetch(
    request: Request,
    env: Env,
    ctx: ExecutionContext
  ): Promise<Response> {
    const client = new Client({ connectionString: env.DATABASE_URL });
    await client.connect();
    const db = drizzle(client);
    const result = await db.select().from(...);

    // Clean up the client, ensuring we don't kill the worker before that is completed.
    ctx.waitUntil(client.end());
    return new Response(now);
  }
}
⚙️

For the built in migrate function with DDL migrations we strongly encourage you to use max: 1 connection configuration.

For querying purposes feel free to use pool size of your choice based on your business demands.

Vercel Postgres

According to their official website, Vercel Postgres is a serverless SQL database designed to integrate with Vercel Functions.

Drizzle ORM natively supports both @vercel/postgres serverless driver with drizzle-orm/vercel-postgres package and postgres or pg drivers to access Vercel Postgres through postgesql://

Check out the official Vercel Postgres + Drizzle docs.

Install dependencies

npm
yarn
pnpm
bun
npm i drizzle-orm @vercel/postgres
npm i -D drizzle-kit

Prepare Vercel Postgres

Setup a project according to the official docs.

Make your first query

index.ts
import { sql } from '@vercel/postgres';
import { drizzle } from 'drizzle-orm/vercel-postgres';

const db = drizzle(sql)
const result = await db.select().from(...);

With @vercel/postgres severless package you can access Vercel Postgres from either serverful or serverless environments with no TCP available, like Cloudflare Workers, through websockets.

If you’re about to use Vercel Postgres from a serverfull environment, you can do it either with @vercel/postgres or directly access the DB through postgesql:// with either postgres or pg.

Supabase

According to the official website, Supabase is an open source Firebase alternative for building secure and performant Postgres backends with minimal configuration.

Checkout official Supabase + Drizzle docs.

Install dependencies

npm
yarn
pnpm
bun
npm i drizzle-orm postgres
npm i -D drizzle-kit

Create your models

schema.ts
import { pgTable, serial, text, varchar } from "drizzle-orm/pg-core";

export const users = pgTable('users', {
  id: serial('id').primaryKey(),
  fullName: text('full_name'),
  phone: varchar('phone', { length: 256 }),
});

Make your first query

index.ts
import { drizzle } from 'drizzle-orm/postgres-js'
import postgres from 'postgres'
import { users } from './schema'

const connectionString = process.env.DATABASE_URL

const client = postgres(connectionString)
const db = drizzle(client);

const allUsers = await db.select().from(users);

Connection pooling (optional)

If you decide to use connection pooling via Supabase (described here), and have “Transaction” pool mode enabled, then ensure to turn off prepare, as prepared statements are not supported.

index.ts
import { drizzle } from 'drizzle-orm/postgres-js'
import postgres from 'postgres'
import { users } from './schema'

const connectionString = process.env.DATABASE_URL

// Disable prefetch as it is not supported for "Transaction" pool mode 
const client = postgres(connectionString, { prepare: false })
const db = drizzle(client);

const allUsers = await db.select().from(users);

Connect to your database using the Connection Pooler for serverless environments, and the Direct Connection for long-running servers.

AWS Data API

Drizzle ORM natively supports aws-sdk driver with drizzle-orm/aws-data-api package.

npm
yarn
pnpm
bun
npm i drizzle-orm @aws-sdk/client-rds-data @aws-sdk/credential-providers
npm i -D drizzle-kit
index.ts
import { drizzle } from 'drizzle-orm/aws-data-api/pg';
import { RDSDataClient } from '@aws-sdk/client-rds-data';
import { fromIni } from '@aws-sdk/credential-providers';

const rdsClient = new RDSDataClient({
    credentials: fromIni({ profile: process.env['PROFILE'] }),
    region: 'us-east-1',
});

const db = drizzle(rdsClient, {
  database: process.env['DATABASE']!,
  secretArn: process.env['SECRET_ARN']!,
  resourceArn: process.env['RESOURCE_ARN']!,
});

await db.select().from(...)...;

HTTP proxy

Example of driver implementation

import { drizzle } from 'drizzle-orm/pg-proxy';

const db = drizzle(async (sql, params, method) => {
  try {
    const rows = await axios.post('http://localhost:3000/query', { sql, params, method });

    return { rows: rows.data };
  } catch (e: any) {
    console.error('Error from pg proxy server: ', e.response.data)
    return { rows: [] };
  }
});

Example of server implementation

import { Client } from 'pg';
import express from 'express';

const app = express();

app.use(express.json());
const port = 3000;

const client = new Client('postgres://postgres:postgres@localhost:5432/postgres');

app.post('/query', async (req, res) => {
  const { sql, params, method } = req.body;

  // prevent multiple queries
  const sqlBody = sql.replace(/;/g, '');

    try {
        const result = await client.query({
            text: sqlBody,
            values: params,
            rowMode: method === 'all' ? 'array': undefined,
        });
    res.send(result.rows);
  } catch (e: any) {
    res.status(500).json({ error: e });
  }

  res.status(500).json({ error: 'Unknown method value' });
});

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`);
});