Skip to content

Instantly share code, notes, and snippets.

What would you like to do?

Deploy JSON API to Heroku


By the end of this breakout, you will be able to:

  1. Set up an application on heroku
  2. Provision a postgres DB on Heroku
  3. Run knex commands to create the schema and seed a production database

Deploy to Heroku

  • Install the herkou CLI

brew install heroku

  • Signup and login to heroku

heroku login

  • Create a heroku app

heroku create

  • Push to heroku

git push heroku master

  • Open the URL from the command line

heroku open

  • View heroku logs

heroku logs --tail

Add Postgres DB to Heroku

  • Add postgres addon

heroku addons:create heroku-postgresql:hobby-dev

  • Add production connection to knex
    • Add a production enviornment to the knex.js file in your root directory. The connection should be set to process.env.DATABASE_URL
    • Look in your db/knex.js file and ensure your connection variable is set to process.env.NODE_ENV || developement

Add a .env file

  • Download the npm package dotenv

npm install dotenv

  • Require the pacakge in your knexfile.js


  • Make a .env file

touch .env

  • Add your .env file to your .gitignore file

  • Add your database url to your .env file

    • Find your app on heroku > settings > config variables


Run Migrations and Seeds on production DB

heroku run knex migrate:latest

  • Use psql to examine tables and data on the production DB

heroku pg:psql

  • Run seed on production DB

heroku run knex seed:run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment