rembrembdocs

This guide assumes familiarity with:

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:

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
yarn add drizzle-orm pg -D drizzle-kit @types/pg
pnpm add drizzle-orm pg -D drizzle-kit @types/pg
bun add drizzle-orm pg -D drizzle-kit @types/pg

Step 2 - Initialize the driver and make a query

Connection URL

With config

With existing client

import { drizzle } from 'drizzle-orm/node-postgres';

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

const result = await db.execute('select 1');
import { drizzle } from 'drizzle-orm/node-postgres';

const db = drizzle({
  connection: {
    connectionString: process.env.DATABASE_URL,
    ssl: true
  }
});

const result = await db.execute('select 1');
import { drizzle } from "drizzle-orm/node-postgres";
import { Pool } from "pg";

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

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:

Step 1 - Install packages

npm i drizzle-orm @neondatabase/serverless -D drizzle-kit
yarn add drizzle-orm @neondatabase/serverless -D drizzle-kit
pnpm add drizzle-orm @neondatabase/serverless -D drizzle-kit
bun add 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');

Connection URL format

postgresql://{username}:{password}@{host}:{port}/postgres?sslmode=verify-full

Connection ports

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.

What’s next?