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.
- Create a new Heroku account or sign in into an existing account.
- Go to your apps dashboard: https://dashboard.heroku.com/apps.
- To create a new app, click New > Create new app (right-hand side).
- On the next page, enter the app name and click the Create App button.
Now that you have created a Heroku app, the new step is to add a database to this app.
- Inside the newly created app, switch to the Resources tab.
- Under Add-ons, search for Heroku Postgres and then select from the suggested list.
- In the popup shown, select free Hobby Dev - Free plan, click Provision.
- Click on the just added database (Heroku Postgres :: Database).
- 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:
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.
- Go to the API Express page.
- Click the Create new DB connection button.
- Enter a connection name and click Create:
- Setup the connection using your Heroku database credentials:
- 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.
Updated about 1 year ago