Drizzle <> PlanetScale Postgres
- Database connection basics with Drizzle
- PlanetScale Postgres database - docs
- Drizzle PostgreSQL drivers - docs
PlanetScale offers both MySQL (Vitess) and PostgreSQL databases. This page covers connecting to PlanetScale Postgres.
For PlanetScale MySQL, see the PlanetScale MySQL connection guide.
With Drizzle ORM you can connect to PlanetScale Postgres using:
- The standard
node-postgresdriver - The
@neondatabase/serverlessdriver for serverless environments
For detailed instructions on creating a PlanetScale Postgres database and obtaining credentials, see the PlanetScale Postgres documentation.
node-postgres
Step 1 - Install packages
npm i drizzle-orm pg -D drizzle-kit @types/pg
Step 2 - Initialize the driver and make a query
import { drizzle } from 'drizzle-orm/node-postgres';
const db = drizzle(process.env.DATABASE_URL);
const result = await db.execute('select 1');
Neon serverless driver
PlanetScale Postgres also supports connections via the Neon serverless driver. This is a good option for serverless environments like Vercel Functions, Cloudflare Workers, or AWS Lambda.
The driver supports two modes:
- HTTP mode — Faster for single queries and non-interactive transactions
- WebSocket mode — Required for interactive transactions or session-based features
Step 1 - Install packages
npm i drizzle-orm @neondatabase/serverless -D drizzle-kit
Step 2 - Initialize the driver and make a query
import { neon, neonConfig } from '@neondatabase/serverless';
import { drizzle } from 'drizzle-orm/neon-http';
// Required for PlanetScale Postgres connections
neonConfig.fetchEndpoint = (host) => `https://${host}/sql`;
const sql = neon(process.env.DATABASE_URL!);
const db = drizzle({ client: sql });
const result = await db.execute('select 1');
PlanetScale Postgres supports two connection ports:
5432: Direct connection to PostgreSQL. Total connections are limited by your cluster’s max_connections setting.
6432: Connection via PgBouncer for connection pooling. Recommended when you have many simultaneous connections.