Connect a Next.js application to PlanetScale
Spin up a PlanetScale MySQL serverless database in seconds and connect to a Next.js application
In this tutorial, you'll create a Next.js application that uses Tailwind CSS for styling and Prisma to connect to a PlanetScale database.
Prerequisites
- Node.js
- PlanetScale CLI
- Authenticate the CLI with the following command:
Set up the database
Create a new database with the following command:
A branch, main
, was automatically created when you created your database, so you can use that for BRANCH_NAME
in the steps below.
Set up the starter Next.js app
Clone the starter repository.
Install the dependencies.
Next, you'll need to create a database username and password through the CLI to connect to your application. If you'd prefer to use the dashboard for this step, you can find those instructions in the Connection Strings documentation and then come back here to finish setup.
First, create your .env
file by renaming the .env.example
file to .env
:
Next, using the PlanetScale CLI, create a new username and password for the branch of your database:
The PASSWORD_NAME
value represents the name of the username and password being generated. You can have multiple credentials for a branch, so this gives you a way to categorize them. To manage your passwords in the dashboard, go to your database overview page, click "Settings", and then click "Passwords".
Take note of the values returned to you, as you won't be able to see this password again.
You'll use these properties to construct your connection string, which will be the value for DATABASE_URL
in your .env
file. Update the DATABASE_URL
property with your connection string in the following format:
Push the database schema to your PlanetScale database using Prisma.
Run the seed script to populate your database with Product
and Category
data.
Run the app
Run the app with following command:
Open your browser at localhost:3000 to see the running application.
Deploying
After you've got your application running locally, it's time to deploy it. To do so, you'll need to promote your database branch (main
by default) to be the production branch (read the branching documentation for more information).
Now that your branch has been promoted to production, you can either use the existing password you generated earlier for running locally or create a new password. Regardless, you'll need a password in the deployment steps below.
Choose one of the following deploy buttons and make sure to update the DATABASE_URL
variable during this setup process.
Deploy on Vercel
Walk through the steps listed on the Vercel deployment page. For more in depth instructions, see our Deploy to Vercel documentation.
Deploy on Netlify
*Note: The Netlify.toml
file in this repository includes the configuration for you to customize the DATABASE_URL
property on the initial deploy.
Follow the instructions on the Netlify deploy page. For more in depth instructions, see our Netlify deployment documentation.
Learn more
To learn more about PlanetScale, take a look at the following resources:
- PlanetScale quickstart guide — Learn how to get started with PlanetScale.
- PlanetScale branching
What's next?
Learn more about how PlanetScale allows you to make non-blocking schema changes to your database tables without locking or causing downtime for production databases. If you're interested in learning how to secure your application when connecting to PlanetScale, please read Connecting to PlanetScale securely.
Need help?
Get help from PlanetScale's support team, or join our GitHub Discussion board to see how others are using PlanetScale.