Trusted answers to developer questions

How to Push a Local PostgreSQL Database to Heroku

Get the Learn to Code Starter Pack

Break into tech with the logic & computer science skills you’d learn in a bootcamp or university — at a fraction of the cost. Educative's hand-on curriculum is perfect for new learners hoping to launch a career.

Most times, when we’re deploying our web application to Heroku, we follow a workflow where we first create the complete application on our local server and then deploy it to Heroku. In such situations, we often need to deploy our local database to Heroku as well. In this article, we will discuss how to deploy an already created PostgreSQL database to Heroku.

Prerequisites

For this article, we’re assuming that we have a PostgreSQL database and we want to deploy it to Heroku for our web application to work properly. This deployment involves the following two steps:

  1. Creating a PostgreSQL database on Heroku
  2. Pushing the local PostgreSQL database to Heroku

If you want to see the whole process of deploying a web application to Heroku, please go to this project to learn how to deploy a web application to Heroku step by step.

Let’s learn how to push our local PostgreSQL database to Heroku.

Creating a database on Heroku

The following command creates a PostgreSQL database on Heroku for our application:

heroku addons:create heroku-postgresql:hobby-dev

After executing the command given above, we will see the following output:

Creating PostgreSQL database on Heroku

Our PostgreSQL database on Heroku is identified by a unique name called “DATABASE_URL”, that is highlighted in red in the image above. We’ll use this name in the next step to push our database to Heroku.

Pushing our local database to Heroku

Finally, we’ll push our local database to the database on Heroku that we just created. We’ll put the Heroku database name, which we obtained in the previous step, in place of heroku-db-name:

PGUSER=postgres PGPASSWORD=password123  heroku pg:push postgres://localhost/example <heroku-db-name>

The rest of the parameters in the command above are pretty straightforward and are explained below:

  • PGUSER refers to the PostgreSQL database username.
  • PGPASSWORD refers to the PostgreSQL database password.
  • example refers to the name of the local PostgreSQL database.

RELATED TAGS

cloud deployment
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?