rembrembdocs

PlanetScale offers both MySQL (Vitess) and PostgreSQL databases. This page covers connecting to PlanetScale MySQL.

For PlanetScale Postgres, see the PlanetScale Postgres connection guide.

With Drizzle ORM you can access PlanetScale MySQL over http through their official database-js driver from serverless and serverfull environments with our drizzle-orm/planetscale-serverless package.

You can also access PlanetScale MySQL through TCP with mysql2 driver — see here.

Step 1 - Install packages

npm i drizzle-orm @planetscale/database -D drizzle-kit
yarn add drizzle-orm @planetscale/database -D drizzle-kit
pnpm add drizzle-orm @planetscale/database -D drizzle-kit
bun add drizzle-orm @planetscale/database -D drizzle-kit

Step 2 - Initialize the driver and make a query

import { drizzle } from "drizzle-orm/planetscale-serverless";

const db = drizzle({ connection: {
  host: process.env["DATABASE_HOST"],
  username: process.env["DATABASE_USERNAME"],
  password: process.env["DATABASE_PASSWORD"],
}});

const response = await db.select().from(...)

If you need to provide your existing driver

import { drizzle } from "drizzle-orm/planetscale-serverless";
import { Client } from "@planetscale/database";

const client = new Client({
  host: process.env["DATABASE_HOST"],
  username: process.env["DATABASE_USERNAME"],
  password: process.env["DATABASE_PASSWORD"],
});

const db = drizzle({ client });

Make sure to checkout the PlanetScale official MySQL courses, we think they’re outstanding 🙌

What’s next?