Deploy a PostgreSQL Database on Heroku
Learn how to deploy a free PostgreSQL Database on Heroku
Heroku Postgres is an SQL database service that allows you to use a relational database such as PostgreSQL on the Heroku platform.
The Heroku Postgres service is a great option when you need a hosted database for your projects, MVPs or proofs-of-concept.
Heroku has several pricing plans for this service, including a free plan. This article teaches you how to deploy a free PostgreSQL database on Heroku.
Create a new application
The first step is to create a new application on Heroku. To do that, log in to your Heroku account and go to the dashboard.
You can either access your existing applications from the dashboard (if you have any) or create new applications.
In this case, you need to create a new application for the database. Click on the "New" button and choose "Create new app", as shown in figure 1.
After that, it prompts you to:
- Choose a name for your application
- Select a region
Once you choose a name and a region for your application, press on "Create app".
After clicking the button, Heroku creates the application in the background. It takes seconds to create the application, and then it takes you to the app's dashboard.
Figure 3 illustrates my application's dashboard.
The next step is to add PostgreSQL to the application.
You can install add-ons to your application from the "Resources" page. In figure 3, you can see the option "Resources" next to "Overview" and under the application name. Click on it.
Once you are on the "Resources" page, go to the "Add-ons" section and search for "Postgres", as shown in figure 4.
From the drop-down, choose the option "Heroku Postgres". When you click on it, a new pop-up appears from where you can select plans. Figure 5 illustrates the pop-up.
Hobby Dev plan, which is the free plan. This plan is more than enough for side projects, MVPs or proofs-of-concept.
Click "Submit Order Form" and you are done. The PostgreSQL database is created and ready to be used.
You need to grab the database credentials to connect your applications to the database.
Go back to your application dashboard and click on "Heroku Postgress", as illustrated in figure 6.
That takes you to the database's dashboard page. On this page, you can see information such as:
- the health of the database
- the number of connections
- data size
See figure 7 for reference.
To get the database details, click on the "Settings" option. On the settings page (shown in figure 8), you can:
- reset your database
- destroy your database
- get the database credentials.
The last step is to click on the button "View Credentials..." to get the database information.
Figure 9 illustrates what you should see when you press the "View Credentials..." button.
Connect to the database
There are multiple ways to connect to the database. It depends on what operating system and application you are using.
In my case, I use Postico on MacOS. When using Postico, you can copy the URI (see figure 9) and paste it into your browser's address bar. That automatically creates a database connection in Postico.
Figure 10 illustrates what you should see when you access the URI in the browser. Press "Connect" and you can start using the database.
Alternatively, you can manually enter the database details (from figure 9) in Postico.
From Postico, you can create tables, add data into your database, perform SQL operations, and more.
It may come in handy to learn how to set environment variables on Heroku if you decide to deploy applications on the platform. Alternatively, you might run the "No pg_hba.conf entry for host, SSL off" error when you deploy your Node.js and PostgreSQL application on Heroku.