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

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).
Creating a new app.Creating a new app.

Creating a new app.

  1. On the next page enter the app name and click the Create App button.
Giving app a name.Giving app a name.

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)
)

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.
  4. Setup the connection using your Heroku database credentials:
Connection setup.Connection setup.

Connection setup.

  1. Test the connection and then click Save.

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