Drizzle <> PlanetScale Postgres

This guide assumes familiarity with:

PlanetScale은 MySQL(Vitess)과 PostgreSQL 데이터베이스를 모두 제공합니다. 이 페이지는 PlanetScale Postgres 연결을 다룹니다.

PlanetScale MySQL의 경우 PlanetScale MySQL 연결 가이드를 참조하세요.

Drizzle ORM에서 PlanetScale Postgres에 연결하는 방법:

PlanetScale Postgres 데이터베이스 생성 및 자격 증명 획득에 대한 자세한 내용은 PlanetScale Postgres 문서를 참조하세요.

node-postgres

Step 1 - 패키지 설치

npm
yarn
pnpm
bun
npm i drizzle-orm pg -D drizzle-kit @types/pg

Step 2 - 드라이버 초기화 및 쿼리 실행

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

Neon serverless 드라이버

PlanetScale Postgres는 Neon serverless 드라이버를 통한 연결도 지원합니다. Vercel Functions, Cloudflare Workers, AWS Lambda와 같은 서버리스 환경에 적합합니다.

드라이버는 두 가지 모드를 지원합니다:

Step 1 - 패키지 설치

npm
yarn
pnpm
bun
npm i drizzle-orm @neondatabase/serverless -D drizzle-kit

Step 2 - 드라이버 초기화 및 쿼리 실행

Neon HTTP
Neon WebSockets
import { neon, neonConfig } from '@neondatabase/serverless';
import { drizzle } from 'drizzle-orm/neon-http';

// PlanetScale Postgres 연결에 필요
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');
연결 URL 형식
postgresql://{username}:{password}@{host}:{port}/postgres?sslmode=verify-full
연결 포트

PlanetScale Postgres는 두 가지 연결 포트를 지원합니다:

5432: PostgreSQL 직접 연결. 총 연결 수는 클러스터의 max_connections 설정으로 제한됩니다.

6432: 연결 풀링을 위한 PgBouncer를 통한 연결. 동시 연결이 많은 경우 권장됩니다.

다음 단계