Skip to content

Connect to your database

Understand how to connect your application to your PlanetScale database.

Overview

Now that you have your database set up, the next step is to connect it to your application. Most database systems, including PlanetScale, allow applications to connect using connection strings. Connection strings are strings that contain your database credentials and other connection information needed to connect to your database.

In this guide, you’ll learn how to generate a connection string for your application and how branches affect the connection strings.

Note

This guide continues from the previous entry describing how branching & deployments work. Please follow the steps in that guide before proceeding.

Create a connection string

Open the beam-demo database and click the "Connect" button.

The following modal will provide the information required to connect to your database. If this is your first time accessing the Connect modal, the password will be displayed in plain text, otherwise, the password will be masked.

If you need to generate a new password, simply click the "New password" button to generate a new set of credentials.

If you want a language/framework-specific connection string, you can use the "Connect with" dropdown and pick the option that best suits what you are building with.

Selecting any of these options not only provides the connection string specific to your environment, but also a sample file that can be used in your code. For example, if you select "Go" from the list of options, you’ll be provided a sample .env file that contains the connection string as well as a main.go file that has the necessary code to connect to PlanetScale using that connection string.

Branches and connection strings

Connection strings allow your application to securely connect to PlanetScale, but since branches are effectively separate copies of your database, it’s important to know how to generate a connection string for the branch you want to connect to. In the above section, we generated a connection string from the Dashboard tab of the database. This will create a connection string to the default main branch, which is our production database.

When you're working in development, you'll want to connect your dev (local or staging) application to a PlanetScale development branch. To create a connection string for another branch, simply navigate to the branch, and click the Connect button from the overview tab of that branch. If you already have existing credentials that you've created in the past, those will be listed below (excluding the password). If you need to generate new credentials, click "New password".

What's next?

At this point, you should have a good understanding of how to work with and manage databases on PlanetScale, including branching, deployments, and connecting to your database.

Use the resources below to determine next steps for your application.

Connect your code

Use one of the following quick-starts to get up and running using your preferred language/framework.

Import your database

Learn how to import an existing MySQL database from supported cloud providers.

Need help?

Get help from the PlanetScale support team, or join our GitHub discussion board to see how others are using PlanetScale.

Was this page useful?
Last updated on Help us improve this page