Back to Drizzle Orm

Drizzle PlanetScale Postgres

src/content/docs/connect-planetscale-postgres.mdx

latest4.6 KB
Original Source

import Npm from "@mdx/Npm.astro"; import Callout from "@mdx/Callout.astro"; import CodeTabs from "@mdx/CodeTabs.astro"; import Section from "@mdx/Section.astro"; import Prerequisites from "@mdx/Prerequisites.astro"; import WhatsNextPostgres from "@mdx/WhatsNextPostgres.astro";

Drizzle <> PlanetScale Postgres

<Prerequisites> </Prerequisites>

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-postgres driver
  • The @neondatabase/serverless driver 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>drizzle-orm pg -D drizzle-kit @types/pg</Npm>

Step 2 - Initialize the driver and make a query

<CodeTabs items={["Connection URL", "With config", "With existing client"]}>

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

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

const result = await db.execute('select 1');

typescript
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');
typescript
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");
</CodeTabs>

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>drizzle-orm @neondatabase/serverless -D drizzle-kit</Npm>

Step 2 - Initialize the driver and make a query

<CodeTabs items={["Neon HTTP", "Neon WebSockets"]}>

typescript
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');

<Section> ```typescript copy import { Pool, neonConfig } from '@neondatabase/serverless'; import { drizzle } from 'drizzle-orm/neon-serverless';

// Required for PlanetScale Postgres connections neonConfig.pipelineConnect = false; neonConfig.wsProxy = (host, port) => ${host}/v2?address=${host}:${port};

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

const result = await db.execute('select 1');


```typescript copy
// For Node.js environments - install 'ws' package
import ws from "ws";
import { Pool, neonConfig } from "@neondatabase/serverless";
import { drizzle } from "drizzle-orm/neon-serverless";

neonConfig.webSocketConstructor = ws;
// Required for PlanetScale Postgres connections
neonConfig.pipelineConnect = false;
neonConfig.wsProxy = (host, port) => `${host}/v2?address=${host}:${port}`;

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

const result = await db.execute("select 1");
```

</Section>
</CodeTabs>

<Callout title="Connection URL format">
  {`postgresql://{username}:{password}@{host}:{port}/postgres?sslmode=verify-full`}
</Callout>

<Callout title="Connection ports" type="info">
  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.

  </Callout>

#### What's next?

<WhatsNextPostgres />