{ Adding a Postgres Database to Heroku. }


By the end of this chapter, you should be able to:

  • Add a Postgres database to a Heroku application
  • Debug a Heroku application using heroku logs

Deploying and Adding Postgres

To add a production database with postgres, we can use a Heroku addon! In the root directory of your application, type the following: heroku addons:create heroku-postgresql:hobby-dev. They may ask you for a credit card, but the layer we'll be using is entirely free. Once you have that installed you will get a config variable called DATABASE_URL which you can see by typing heroku config in the terminal.

Inside of our db/index.js, we need to now add the following line to tell our application to connect to the production database or development database, depending on the environment. Here is what that could look like:

// instead of...
const client = new Client({
  connectionString: "postgresql://localhost/deploy_me"
// we now do...
const client = new Client({
    process.env.DATABASE_URL || "postgresql://localhost/deploy_me"

For more information, check out this guide on Heroku's site.


If you are having issues deploying your application or it is crashing, make sure you run heroku logs -t -a NAME_OF_YOUR_APPLICATION to see the server logs and debug from there. Any time you make a change remember you need to commit that change and push it to Heroku.

When you're ready, move on to Server-side Templating with Pug


Creative Commons License