Connect a Next.js application to PlanetScale
Spin up a PlanetScale MySQL database in seconds and connect to a Next.js application
If this is your first time in the dashboard, you'll be prompted to go through a database creation walkthrough where you'll create a new database. Otherwise, click "New database" > "Create new database".
- Name — You can use any name with lowercase, alphanumeric characters, or underscores. We also permit dashes, but don't recommend them, as they may need to be escaped in some instances.
- Plan type — Select the desired plan for your database.
- Region — Choose the region closest to you or your application. It's important to note if you intend to make this branch a production branch, you will not be able to change the region later, so choose the region with this in mind.
Finally, click "Create database".
If you have an existing cloud-hosted database, you can also choose the "Import" option to import your database to PlanetScale using our Import tool. If you go this route, we recommend using our Database Imports documentation.
A production branch,
main, is automatically created when you create your database. Production branches are highly available, protected database that you can connect your production application to. Once you are satisfied with your initial development, you may enable safe migrations to enable zero-downtime migrations and protect the branch from accidental data deletion.
Now that you have your database, clone the Next.js starter repository, or grab your own project.
git clone https://github.com/planetscale/nextjs-starter
Install the dependencies with:
.env file by renaming the
.env.example file to
mv .env.example .env
Next, you need to generate a database username and password so that you can use it to connect to your application.
In your PlanetScale dashboard, select your database, click "Connect", and select "Prisma" from the "Connect with" dropdown.
As long as you're an organization administrator, this will generate a username and password that has administrator privileges to the database.
DATABASE_URL string from the
.env tab and paste it into your own
.env file. The structure will look like this:
Your PlanetScale database should now be connected to your application.
With the database connected, you're now ready to add a schema, and read/write data.
The sample repo we shared above includes a pre-built schema and some seed data built with Prisma. Push the database schema to your PlanetScale database with:
npx prisma db push
Run the seed script. This will populate your database with
npm run seed
Run the app with following command:
npm run dev
Open your browser at localhost:3000 to see the running application.
After you have your application running locally, you may want to deploy it to production. Your database branch (
main by default) is already a production database branch. You should also enable safe migrations, which protects your production branch from accidental schema changes. This can be done from the PlanetScale dashboard by clicking the same "cog" once the branch has been promoted to production.
In the modal that will appear, toggle the option labeled "Enable safe migrations", then click the "Enable safe migrations". This will close the modal and save the setting.
If you'd like to deploy to Vercel, check out our Deploy to Vercel documentation.
If you'd like to deploy to Netlify, check out our Deploy to Netlify documentation.
If you are deploying the
nextjs-starter repo, the
Netlify.toml file in this repository includes the configuration for you to customize the
PLANETSCALE_PRISMA_DATABASE_URL property on the initial deploy.
To learn more about PlanetScale, take a look at the following resources:
- PlanetScale workflow — Quick overview of the PlanetScale workflow: branching, non-blocking schema changes, deploy requests, and reverting a schema change.
- PlanetScale branching — Learn how to utilize branching to ship schema changes with no locking or downtime.
- PlanetScale CLI — Power up your workflow with the PlanetScale CLI. Every single action you just performed in this quickstart (and much more) can also be done with the CLI.