Setup Free PostgreSQL on Heroku
Tags: heroku, postgres, sql
Published: 2022-01-14 03:22:32

Who don't want a free charge hosted database to test an app? I think anyone wants something free to test their app. Especially, a small app. Why do we buy a VPS just for testing a small scale hosted database when there is a free solution like Heroku PostgreSQL.

## How To Host A PostgreSQL on Heroku For Free

We will cover step by step to host a PostgreSQL on Heroku. **First of all**, make sure you are already registered on Heroku. If not, please just register on the website, [https://heroku.com](https://heroku.com). After that, just login the website with your registered account.

### Step One : Create an app on Heroku

To create an app in Heroku, you can click button **New** > **Create New App** or if you are new in Heroku, you can just click button **Create a new app** button on your Heroku desktop.
 
![Heroku dashboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kr2txvid2f0u0o4ntgjq.png)

After that, the Create New App page will be shown.

![Create New App page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n2chj4u135luqjhehprl.PNG)

Fill the App name with the name you want until you get a green tick. In my case, I will name it **postgresql-app**. You can choose your **region** between United States or Europe to host your database. Then, click **Create App**. You'll be redirected to the app dashboard page after that.

### Step Two : Add PostgreSQL Database.
To add a database to your app, go to **Resources** on your app dashboard. Then, on the Add-ons, search for **Heroku Postgres**.

You will get a pop up to submit your order.
![Order submission form pop up](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/woj53anstowwl0kt9fqn.png)

Choose **Hobby Dev - Free** on Plan name select. Then, click **Submit Order Form** button.

Your database app is successfully created on Heroku! Congrats!

### Step Three : Database Credentials (How to obtain it)
Now, you have your database app. But, how to access it? To access your hosted database from your client app, you need some credentials variables such us server URL, port number, username, password, and database name.

![Our Heroku Postgres app dashboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j0jmmzel0f5dh4j3mc64.png)

To obtain your database app credentials, click the **Heroku Postgres** on your app dashboard. You will see the Datastores page like below.

![Datastores page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kcqi2dhcewwm7top96v0.png)

Go to **Settings**, Click on **View Credentials...**
![Click on View Credentials... button](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j87f14u2jzbwmcckqapk.png)

And, there you have it. You got your database app credential.
![Database app credential, I don't blur the Port because 5432 is PostgreSQL default port](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ghse2grd2wbljxfjz0t.png)
You can copy these information to your apps or your database clients to access it.

## Bonus : Connect to DBeaver Database Client.

DBeaver is free and open-source database client app that can integrates with various DBMS including PostgreSQL.

![DBeaver logo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d1b06ssx8mjngw1gbohx.png)

First of all, you absolutely need to install DBeaver first. You can get it from its [website](https://dbeaver.io/), you can install in Windows, Linux, or MacOS system. I won't cover the installation on this article as it is easy to find the tutorial on the internet if you are confused.

Further, open your installed DBeaver. Click **File** > **New** or just type the shortcut **CTRL + N**. You will get **Select a wizard** pop up. Select **DBeaver** > **Database Connection**. Then, click **Next**.

![Select a wizard pop up](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vfwo21tow3fei752t6ey.png)

Next, **Select SQL** > **PostgreSQL** on **Select your database** part. Then, click **Next**.

![Select your database](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3dpfkb7tum59acglfo5v.png)

Next, on **Connection Settings**, copy your database credentials (host, database, user / username, and password) from your Heroku database app to this form. Then, click **Finish**.

![Connection Settings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rvv2jznpefdb3peky1wm.png)

To connect or check the connection, you can right click to the database on DBeaver **Database Navigator**, select **Connect**.

![How to connect](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/675povrci719g4lrmnu3.png)
 
If you get PostgreSQL with green tick next to your database name on **Database Navigator** like in the picture below, that means you are successfully connected to your Heroku PostgreSQL app.

![Successful connection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bd6mkpbzpq9r0x8wz8u6.png)
 
That's it. :)