Cover image for How To Deploy To Heroku Using Postgres

How To Deploy To Heroku Using Postgres

sandravaphilips profile image Sandrava Philips ・2 min read

So you've designed your database, built it and it's now running locally on your desktop with no errors. The only thing left is deploying to Heroku. Well, this post provides some guidance on how to do that.

  1. I'm assuming you already have an account with Heroku and if not, you can sign up here. What you have to do next is install pg, a Postgres client for NodeJs.

    npm install pg
  2. Add a Knex configuration for production

    production: {
        client: 'pg',
        connection: process.env.DATABASE_URL,
        migrations: {
            directory: './data/migrations',
        seeds: { directory: './data/seeds' },

    I'll explain where the connection value is coming from later.

  3. Log in to Heroku, click New to create a new app, give your app a name and create it.

  4. Choose a deployment method. Since we're going to be using Heroku commands to run knex migrations, let's use the Heroku Git deployment method. The instructions on how to install and use Heroku CLI are simple and easy to follow.

  5. Once you're through installing Heroku CLI and running the following commands, move to the Resources tab and type Heroku Postgres in the add-ons search bar. Click on it and provision the plan you prefer.

  6. Navigate to the Settings tab and click to reveal Config Vars. It should already have your DATABASE_URL. If not, go to the Overview tab and you should see your Heroku Postgres add-on, click on it to see all the information about your database. On the same page, go to the Settings tab to view your credentials. There you'll see your URI. Copy that and paste it as a value in your Config Vars with DATABASE_URL as the key. Note: Your key can be any valid name, just ensure it matches the name in your knex configuration. You can also add more config vars like the environment you wish to work with, i.e, production.

  7. Review all your configurations. Make sure your knex configuration environment is not hardcoded to development. It should be similar to this:

    const knex = require('knex');
    const knexConfig = require('./knexfile');
    const environment = process.env.DB_ENV || 'development';
    module.exports = knex(knexConfig[environment]);

    Your port number should also not be harcoded.

    const port = process.env.PORT || 5000;

    Of course, you can include the DB_ENV value in your Config Vars on

  8. Next, run the following command:

    heroku run knex migrate:latest

And you're good to go. You can view the url of your deployed app by clicking on Open app on Heroku or scroll down to the domains section of the Settings tab.

Well done, champ. Now you can go grab that beer. Don't forget to let me know if you found this helpful, though.

Till next time......


Editor guide
thisdotmedia_staff profile image
This Dot Media

Thanks for breaking it down so flawlessy! Awesome article Sandrava 🤗