src/content/docs/connect-planetscale-postgres.mdx
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";
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:
node-postgres driver@neondatabase/serverless driver for serverless environmentsFor detailed instructions on creating a PlanetScale Postgres database and obtaining credentials, see the PlanetScale Postgres documentation.
<Npm>drizzle-orm pg -D drizzle-kit @types/pg</Npm>
<CodeTabs items={["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");
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:
<Npm>drizzle-orm @neondatabase/serverless -D drizzle-kit</Npm>
<CodeTabs items={["Neon HTTP", "Neon WebSockets"]}>
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');
// 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 />