Neon Postgres
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 i drizzle-orm @neondatabase/serverless
npm i -D drizzle-kit
yarn add drizzle-orm @neondatabase/serverless
yarn add -D drizzle-kit
pnpm add drizzle-orm @neondatabase/serverless
pnpm add -D drizzle-kit
bun add drizzle-orm @neondatabase/serverless
bun add -D drizzle-kit
With the Neon serverless driver [github, Neon docs],
you can access a Neon database from serverless environments 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.
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(...);
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.
Xata
According their official website, Xata is a Postgres data platform with a focus on reliability, scalability, and developer experience. The Xata Postgres service is currently in beta, please see the Xata docs on how to enable it in your account.
Drizzle ORM natively supports both the xata
driver with drizzle-orm/xata
package and the postgres
or pg
drivers for accessing a Xata Postgres database.
npm i drizzle-orm @xata.io/client
npm i -D drizzle-kit
yarn add drizzle-orm @xata.io/client
yarn add -D drizzle-kit
pnpm add drizzle-orm @xata.io/client
pnpm add -D drizzle-kit
bun add drizzle-orm @xata.io/client
bun add -D drizzle-kit
You can use Drizzle with Xata with a HTTP client or a TCP client. The HTTP client doesn’t create a persistent connection to the Xata server, while the TCP client does and can be used for better performance with a higher number of requests. The HTTP client is usually recommended from serverless environments like Cloudflare Workers or Vercel Edge Functions. The TCP client is typically used from long-running servers like Express.js or Fastify.
The following example use the Xata generated client, which you obtain by running the xata init CLI command.
import { drizzle } from 'drizzle-orm/xata-http';
import { getXataClient } from './xata'; // Generated client
const xata = getXataClient();
const db = drizzle(xata);
const result = await db.select().from(...);
import { drizzle } from 'drizzle-orm/node-postgres';
import { getXataClient } from './xata'; // Generated client
import { Client } from 'pg';
const xata = getXataClient();
const client = new Client({ connectionString: xata.sql.connectionString });
const db = drizzle(client);
import { drizzle } from 'drizzle-orm/node-postgres';
import { getXataClient } from './xata'; // Generated client
import { Pool } from 'pg';
const xata = getXataClient();
const pool = new Pool({ connectionString: xata.sql.connectionString, max: 10 });
const db = drizzle(pool);
If you prefer to not use the generated Xata client, it is also possible to use Xata with the postgres
or pg
drivers, in this case you can copy the connection string from the Settings page of your Xata database. For example:
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: "postgresql://uni123:<YOUR_API_KEY>@us-east-1.sql.xata.sh/mydb:main?sslmode=require",
});
await client.connect();
const db = drizzle(client);
For more details about using Drizzle with Xata, see the official Xata docs.
PGlite
According to the official repo, PGlite is a WASM Postgres build packaged into a TypeScript client library that enables you to run Postgres in the browser, Node.js and Bun, with no need to install any other dependencies. It is only 2.6mb gzipped.
It can be used as an ephemeral in-memory database, or with persistence either to the file system (Node/Bun) or indexedDB (Browser).
Unlike previous “Postgres in the browser” projects, PGlite does not use a Linux virtual machine - it is simply Postgres in WASM.
npm i drizzle-orm @electric-sql/pglite
npm i -D drizzle-kit
yarn add drizzle-orm @electric-sql/pglite
yarn add -D drizzle-kit
pnpm add drizzle-orm @electric-sql/pglite
pnpm add -D drizzle-kit
bun add drizzle-orm @electric-sql/pglite
bun add -D drizzle-kit
import { PGlite } from '@electric-sql/pglite';
import { drizzle } from 'drizzle-orm/pglite';
// In-memory Postgres
const client = new PGlite();
const db = drizzle(client);
await db.select().from(users);
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 i drizzle-orm postgres
npm i -D drizzle-kit
yarn add drizzle-orm postgres
yarn add -D drizzle-kit
pnpm add drizzle-orm postgres
pnpm add -D drizzle-kit
bun add drizzle-orm postgres
bun add -D drizzle-kit
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:adminadmin@0.0.0.0:5432/db", { max: 1 });
migrate(drizzle(migrationClient), ...)
// for query purposes
const queryClient = postgres("postgres://postgres:adminadmin@0.0.0.0: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 i drizzle-orm pg
npm i -D drizzle-kit @types/pg
yarn add drizzle-orm pg
yarn add -D drizzle-kit @types/pg
pnpm add drizzle-orm pg
pnpm add -D drizzle-kit @types/pg
bun add drizzle-orm pg
bun add -D drizzle-kit @types/pg
You can connect to a PostgreSQL database either using a single client
connection or a pool
.
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);
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.
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 i drizzle-orm @vercel/postgres
npm i -D drizzle-kit
yarn add drizzle-orm @vercel/postgres
yarn add -D drizzle-kit
pnpm add drizzle-orm @vercel/postgres
pnpm add -D drizzle-kit
bun add drizzle-orm @vercel/postgres
bun add -D drizzle-kit
Prepare Vercel Postgres
Setup a project according to the official docs.
Make your first query
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 i drizzle-orm postgres
npm i -D drizzle-kit
yarn add drizzle-orm postgres
yarn add -D drizzle-kit
pnpm add drizzle-orm postgres
pnpm add -D drizzle-kit
bun add drizzle-orm postgres
bun add -D drizzle-kit
Create your models
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
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.
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 i drizzle-orm @aws-sdk/client-rds-data @aws-sdk/credential-providers
npm i -D drizzle-kit
yarn add drizzle-orm @aws-sdk/client-rds-data @aws-sdk/credential-providers
yarn add -D drizzle-kit
pnpm add drizzle-orm @aws-sdk/client-rds-data @aws-sdk/credential-providers
pnpm add -D drizzle-kit
bun add drizzle-orm @aws-sdk/client-rds-data @aws-sdk/credential-providers
bun add -D drizzle-kit
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}`);
});