DEV Community

Cover image for Deploy PostgreSQL in Node.js to Heroku for beginners
Giovanna Aveiro
Giovanna Aveiro

Posted on • Updated on

Deploy PostgreSQL in Node.js to Heroku for beginners

During my time at Founders and Coders, we had to use Heroku to deploy our Node + PostgreSQL projects quite a few times. Because it can be daunting at first, I put together this step-by-step based on their graphic user interface. It is quick and straight to the point. Questions and contributions are welcome!

Deploy your project

Signup/Login to www.heroku.com. Go to New > Create new app to start. Add name, region and hit Create app. Choose GitHub as a deployment method under the 'Deploy' tab, search for your repo to connect:

screenshot

on the same tab, click enable automatic deploys with the master branch for your future deploys. For the first install, hit Deploy Branch in 'Manual deploy' section to deploy your app:

screenshot


Important to notice that after deployment, Heroku will run the 'start' script in your package.json to start your application. Make sure it is correct.

"script": {
    "start": "server.js"
}
Enter fullscreen mode Exit fullscreen mode

Deploy your PostgreSQL database to Heroku

Once your app is up you should see the message below:

screenshot

Go to 'Resources' tab and search for Heroku Postgres add-on, then select the Hobby Dev - Free version and hit Provision to confirm.

screenshot

Back to 'Resources' tab, you can see your current database clicking on Heroku Postgres. You should see in a new tab something similar to the below image, indicating your database is empty:

screenshot

Connect to the database

Heroku will generate a DATABASE_URL that you can use to connect to your production database. You can find this under 'Settings' back to the project tab in Reveal Config Vars with key 'DATABASE_URL'.

Important: If your project/database depend on API keys or tokens, add them in your config vars.

Connect to it via psql/pgcli copying and pasting the url in your terminal and then running you init.sql file:

//in psql

\c <DATABASE_URL>;

\i database/init.sql;
Enter fullscreen mode Exit fullscreen mode

Some extra tips

  • You can view more information about the database by accessing it from 'Resources' and navigating to 'Settings' > View Credentials;
  • You can add collaborators via Accesstab;
  • View logs in More;
  • You can delete projects from Heroku in 'Setting' at the bottom of the page.

Discussion (4)

Collapse
joepock123 profile image
Joe MJ

This is really helpful thank you!

Collapse
glrta profile image
Giovanna Aveiro Author

Thank you for the feedback, Joe!

Collapse
bianoraraujo profile image
Bianor Araujo

Great post! Thanks

Collapse
tomaszwagner1 profile image
TomW

Great read, thank you, Gio!