TipAlready have a Node.js application and just want to connect to PlanetScale? Check out the Node.js quick connect repo.
Prerequisites
Node.js
Set up the database
First, create a new database with the following command:users and add one record to it.
To do this, use the PlanetScale CLI shell to open a MySQL shell where you can manipulate your database. You may need to install the MySQL command line client if you haven’t already.
NoteA branch,
main, was automatically created when you created your database, so you can use that for BRANCH_NAME.users table:
Set up the starter Node.js app
Clone the starter repository:Connect to PlanetScale with Express.js
There are two ways to connect to PlanetScale:- With an auto-generated username and password
- Using the PlanetScale proxy with the CLI
Option 1: Connect with username and password (Recommended)
These instructions show you how to generate a set of credentials with the PlanetScale CLI. You can also get these exact values to copy/paste from your PlanetScale dashboard. In the dashboard, click on the database > “Connect” > “Connect with” language dropdown > “Node.js”. If the password is blurred, click “New password”. Skip to step 3 once you have these credentials.-
Authenticate the CLI with the following command:
-
Using the PlanetScale CLI, create a new username and password for the branch of your database:
Take note of the values returned to you, as you won’t be able to see this password again.NoteThe
PASSWORD_NAMEvalue 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 dashboard page, click “Settings”, and then click “Passwords”. -
Next, create your
.envfile by renaming the.env.examplefile to.env: -
Use the values from the CLI output in step 1 to construct your connection string that will be used to connect your Node app to your PlanetScale database. Create your connection string in the following format:
-
In the
.envfile, fill in theDATABASE_URLvariable with the value you constructed above. It should look something like this: -
Finally, run your Express application with:
users table!
Option 2: Using the PlanetScale proxy with the CLI
Use the following command to create a connection to your database and start the application:NoteRunning
pscale connect with the execute flag will pass a DATABASE_URL to the Node application, enabling it to connect to PlanetScale. Don’t forget to look in app.js to see how the DATABASE_URL is used.users table!

