Deploying a Node.js Postgres/Sequelize App to Heroku

Anjelica A
5 min readJan 31, 2021
Photo by Joaquín Rivero on Unsplash

Today, I have built an app in Node.js/Express that uses PostgreSQL as its database. Last year, Heroku discontinued support for mongolabs, so apps using mongodb may need to find a different add on or switch to a different database. Keep in mind that the database on your localhost will be different than your database on Heroku, so the entries in localhost will not show in the new one.
It is fairly easy to deploy Postgres apps to Heroku, so here’s a step by step on what I did:

1. Setting up the Heroku CLI

Heroku CLI (Command Line Interface): This tool is so we can interact with the heroku service through the terminal and git. I built my app on a mac and I have homebrew installed, so the command I ran was:

$ brew install heroku/brew/heroku

For more information on how to install Heroku CLI: https://devcenter.heroku.com/articles/heroku-cli

2. Login to Heroku

To log in to your Heroku account, you can pull up the terminal and run:

$ heroku login -i

You’ll be prompted to enter your credentials, so enter your user email and your password in the terminal.

3. Create Your Heroku App

After logging in to your Heroku Account, you can create a new Heroku app in your terminal by typingheroku create and then the name of your app afterwards, like so:

$ heroku create your-app-name

This will also add your heroku app as a git remote repository, which you can check with git remote -v in your terminal. By executing this command, you’ll notice two new branches:

heroku "https://git.heroku.com/your-app-name.git" (fetch)
heroku "https://git.heroku.com/your-app-name.git" (push)
origin "https://github.com/your-github-username/your-app-name.git" (fetch)
origin "https://github.com/your-github-username/your-app-name.git" (push)

This is how you’ll know your app has successfully connected to your repository.

4. Adding the Procfile/Start Script

Since Heroku runs npm start by default for node projects, you’ll have to define that script in your package.json. On the top of your package.json,right above “name”, you’ll need to add this:

"scripts": {
"start": "node app.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
Here is how my package.json looks

Basically, this command tells Heroku to run the command node app.js, which is the same command you’d use to run a node app on your terminal.

5. Add the Production Database

You’ll need your app to connect to your Postgres database in order for it to show data, right? In order to do that, you’ll need to start by installing the Heroku Postgres add-on:

$ heroku addons:create heroku-postgresql:hobby-dev

You can check if it installed successfully by going to your Heroku Dashboard and checking your app’s add-ons.

Successfully Installed!

The next step is to re-configure the “Production” section of your config.json. You can find this in your DB folder, in Config. Find the “Production” section and add the following:

"production": {
"use_env_variable": "DATABASE_URL",
"dialect": "postgres",
"dialectOptions": {
"ssl": {
"require": true,
"rejectUnauthorized": false
}
}
}

Do not change the “DATABASE_URL”!

This is how my config.json looks

Now, we’ll need to add a Heroku config variable to signal that we are in Production:

$ heroku config:set NODE_ENV=production

Now would be a good time to commit your changes and push to Heroku. You can do so by running the following commands:

$ git add .
$ git commit -m "added heroku config info"
$ git push heroku

Sometimes, when you run git push heroku,it only pushes to Heroku, so if you want the changes to reflect on your GitHub repo, you’ll have to push twice, but this time with $ git push .

Lastly, we’ll need to migrate our database to Heroku so our database shows properly. The way I did it was by running this command in my terminal:

$ heroku run sequelize db:migrate --env production --app app_name

Make sure you change app_name to the name of your app.
It usually takes a while for your Heroku app to build/migrate the database, but once it’s done, if you head over to your Heroku app URL (the URL is always something like “your-app-name.herokuapp.com”). You can also find it by going to your Heroku Dashboard, clicking your app, and clicking the “open app” button next to the app name.

Once your page loads, you’ll notice that it’s a blank version of your app. Why is the database blank? It’s because you created another Postgres database onto the Heroku app, so you’ll have to manually add in any data entries you had before. Your database on your localhost will be different from your Heroku app database, so keep this in mind.

Conclusion

Photo by krakenimages on Unsplash

Voila! You now have a Postgres database hooked up to your Node.js app. Using these five steps, I was able to deploy my Node.js/Express app with a Postgres database to Heroku. I use Postgres fairly often, and I recommend installing Postico:https://eggerapps.at/postico/ to keep track of your databases and its entries.

--

--