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.
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:
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.
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:
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.
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
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:
PGUSERrefers to the PostgreSQL database username.
PGPASSWORDrefers to the PostgreSQL database password.
examplerefers to the name of the local PostgreSQL database.
View all Courses