Postgres Database On Heroku

Connecting to Postgres hosted on Heroku.

This tutorial shows you how to setup and connect to Postgres database hosted on Heroku.

Creating Heroku App

📘

Want to know more?

You can check out this detailed instruction on how to set up the database with Heroku.

In this section, you will setup a new Heroku app. In the next step, you will add a database to this app. If you want to use an existing app you can skip to the next section.

  1. Create a new Heroku account or sign in into an existing account.
  2. Go to your apps dashboard: https://dashboard.heroku.com/apps.
  3. To create a new app, click New > Create new app (right-hand side).
1225

Creating a new app.

  1. On the next page, enter the app name and click the Create App button.
940

Giving app a name.

Creating Database

Now that you have created a Heroku app, the new step is to add a database to this app.

  1. Inside the newly created app, switch to the Resources tab.
  2. Under Add-ons, search for Heroku Postgres and then select from the suggested list.
  3. In the popup shown, select free Hobby Dev - Free plan, click Provision.
  4. Click on the just added database (Heroku Postgres :: Database).
  5. Scroll towards the bottom of the page and click on the View Credentials button. You will see the database credentials you will need to use when connecting to this database from API Express.

Creating a Table Inside the Database

Now that the database is created you need to create at least one table. You can use any database client of your choice.

We have used pgAdmin database client. Connect to the database using your credentials and the following connection properties:

ssl=true;sslfactory=org.postgresql.ssl.NonValidatingFactory

Then run the following SQL script to create a new table:

CREATE TABLE label
(
   id int PRIMARY KEY NOT NULL,
   name varchar(45)
)

📘

You are free to use other tools for managing the database data.
Please check out the section here that describes how to fill the database with data by using TablePlus.

That's all for setting up the database.

Creating Connection in API Express

In this section, you will create a connection to this database from API Express.

  1. Go to the API Express page.
  2. Click the Create new DB connection button.
  3. Enter a connection name and click Create:
1699
  1. Setup the connection using your Heroku database credentials:
2821

Connection setup.

  1. Test the connection and then click Save:
1029

Now that the connection is active, you can use it when generating API Express services or creating a custom service.