> ## Documentation Index
> Fetch the complete documentation index at: https://docs.zopio.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Switch to PlanetScale

> How to change the database provider to PlanetScale.

Here's how to switch from Neon to [PlanetScale](https://planetscale.com).

## 1. Create a new database on PlanetScale

Once you create a database on PlanetScale, you will get a connection string. It will look something like this:

```
mysql://<username>:<password>@<region>.aws.connect.psdb.cloud/<database>
```

Keep this connection string handy, you will need it in the next step.

## 2. Update your environment variables

Update your environment variables to use the new PlanetScale connection string:

```js apps/database/.env theme={"system"}
DATABASE_URL="mysql://<username>:<password>@<region>.aws.connect.psdb.cloud/<database>"
```

```js apps/app/.env.local theme={"system"}
DATABASE_URL="mysql://<username>:<password>@<region>.aws.connect.psdb.cloud/<database>"
```

Etcetera.

## 3. Swap out the required dependencies in `@repo/database`

Uninstall the existing dependencies...

```sh Terminal theme={"system"}
pnpm remove @neondatabase/serverless @prisma/adapter-neon ws @types/ws --filter @repo/database
```

...and install the new ones:

```sh Terminal theme={"system"}
pnpm add @planetscale/database @prisma/adapter-planetscale --filter @repo/database
```

## 4. Update the database connection code

Update the database connection code to use the new PlanetScale adapter:

```ts packages/database/index.ts {3-4, 17-18} theme={"system"}
import 'server-only';

import { Client, connect } from '@planetscale/database';
import { PrismaPlanetScale } from '@prisma/adapter-planetscale';
import { PrismaClient } from '@prisma/client';
import { env } from '@repo/env';

declare global {
  var cachedPrisma: PrismaClient | undefined;
}

const client = connect({ url: env.DATABASE_URL });
const adapter = new PrismaPlanetScale(client);

export const database = new PrismaClient({ adapter });
```

## 5. Update your Prisma schema

Update your Prisma schema to use the new database provider:

```prisma packages/database/prisma/schema.prisma {10} theme={"system"}
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["driverAdapters"]
}

datasource db {
  provider     = "mysql"
  url          = env("DATABASE_URL")
  relationMode = "prisma"
}

// This is a stub model.
// Delete it and add your own Prisma models.
model Page {
  id    Int     @id @default(autoincrement())
  email String  @unique
  name  String?
}
```

## 6. Add a `dev` script

Add a `dev` script to your `package.json`:

```json packages/database/package.json {3} theme={"system"}
{
  "scripts": {
    "dev": "pscale connect [database_name] [branch_name] --port 3309"
  }
}
```
