Skip to content

Instantly share code, notes, and snippets.

@kimschles
Last active May 3, 2022 05:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save kimschles/5001ee44e25c754f26d752b8f16cc287 to your computer and use it in GitHub Desktop.
Save kimschles/5001ee44e25c754f26d752b8f16cc287 to your computer and use it in GitHub Desktop.
crud-deploy-with-commands.md

Deploy JSON API to Heroku

Objectives

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

require('dotenv').config()

  • 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

DATABASE_URL=39ualdkfj029iepaeuhfglakwjhlfkajhdsp9f8p34k


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