Deploy to Heroku

Introduction

In this tutorial, we're going to deploy an application with a PlanetScale database to Heroku. If you already have an application running locally, you can use it for this tutorial.

Prerequisites

  • Install the PlanetScale CLI.
  • Authenticate the CLI with the following command:
pscale auth login
  • Setup your application to connect to MySQL using the DATABASE_URL environment variable.

Deploy to Heroku

Let's use the pscale CLI on Heroku to establish a secure connection to your database.

  1. Create a PlanetScale service token:
pscale service-token create

Take note of the service token name and value it returns to you.

  1. Grant that token access to your database:
pscale service-token add-access your-token-name connect_production_branch --database your-db-name
  1. Set config vars for your Heroku application:
heroku config:set PLANETSCALE_ORG=your-org-name
heroku config:set PLANETSCALE_SERVICE_TOKEN_NAME=your-token-name
heroku config:set PLANETSCALE_SERVICE_TOKEN=your-token-value
A Note: You'll use your service token to authenticate to your database.
  1. Add the PlanetScale buildpack to your Heroku application:
heroku buildpacks:add https://github.com/planetscale/heroku-buildpack-planetscale

This command will install the latest pscale CLI into your Heroku application. You can specify a CLI version by setting PLANETSCALE_CLI_VERSION

Tip: Find the latest CLI releases here.
  1. Add a Procfile that initiates your application using pscale:
web: pscale connect your-db-name main --execute 'node app.js'
Tip: Heroku runs this command when starting the app.

As you can see, we chose to use the sample Node.js and Express.js application we built in our previous Connect Node.js App tutorial.

This tutorial is language agnostic and may be used with any language.
  1. Push to Heroku and you'll have a working app.

Congratulations! You successfully deployed an application to Heroku.

Summary

In this tutorial, we deployed an application that was connected to a PlanetScale database to Heroku. We chose to use the sample Node.js and Express.js application we built in our previous Connect Node.js App tutorial. That said, you could use any local application you've made and use it to connect to a PlanetScale database.

What's next?

Now that you've successfully deployed an application to Heroku, it's time to make more schema changes to your tables! 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.

Need help?

Get help from PlanetScale's support team, or join our GitHub Discussion board to see how others are using PlanetScale.

Was this page useful?
Last updated on June 16, 2021
PrivacyTerms
© 2021 PlanetScale Inc.