How to Deploy An Express App to Heroku with Postgresql Database Using Git
*Note: This tutorial assumes that you know how to create an express app with database and how to create github repo. If you haven’t created an application yet and you’d like to get a head start, here is a Project Settings for a NodeJS Express app with PSQL.
So you created your first node application, you decided you’re going to show it to your friends who once they’re going to see it, think you’re smart, possibly the smartest person in the room … alright don’t push it, and for you to show it to your friends, you can either run it locally on your machine

Or, you can deploy it somewhere where all your friends can try it. My choice, due to simplicity and habit, is Heroku.
to use heroku, you need to create an account and sign in. Obviously.
Steps
- Open your terminal
- Create a Heroku app using the following command:
$ heroku create
- Grab the link for your application from the response you get on the console.
- Deploy your app using the following command:
$ git push heroku master
- To display logs for Heroku, use the following command:
$ heroku logs --tail
Great, now that you have the link for it, you can just type it in your browser and checkout your website!

But, wait … what about the database mentioned in the fancy title?
Well, first of all .. we need create to a database, if you haven’t created one yet, then checkout the steps here.
The database you create on your laptop, does in fact exist only your laptop
Heroku doesn’t know a thing about it, because that database is for development environment and it is strictly for development and Heroku’s environment is for production. What does that mean? It means we need to head to our knexfile.js and head to the production object and fix/add something there. Here is the current production object:
production: {
client: 'postgresql',
connection: {
database: 'my_db',
user: 'username',
password: 'password'
}We need to have a production environment database that we can reference to in production connection database key. How do we get to create database with our Heroku app? Here is how:
$ heroku addons:create heroku-postgresql:hobby-dev
This creates a database for your app. But how do I access this database? Type this command:
$ heroku config
You’ll get something similar to this:

See that DATABASE_URL? That’s a link to your production environment database that was create for you by Heroku after the previous command. Copy that URL and head to your knexfile.js, to production object and paste the url to database connection key.
Make sure you push your changes to your master, then deploy the changes to Heroku like you did before. Open your Heroku app.
- To create run the migrations that you have created, you need to run them for heroku using the command:
$ heroku run knex migrate:latest
- You should run that command above whenever you have a new migration.
Mission accomplished.