Using Heroku Postgres From A Fly App

Heroku supports applications with no apps in them and only add-ons, like Postgres, as a way of providing those services to other applications. As we showed, you can access Heroku Postgres from Fly so it makes it a useful way to get yourself a databa

Dj Walker-Morgan
5 min readintermediate
--
View Original

Overview

This article explains how to use Heroku Postgres with a Fly application without migrating the application from Heroku. It provides a step-by-step guide on setting up the database, obtaining credentials, and connecting to the database using Node.js and Sequelize.

What You'll Learn

1

How to set up a Heroku Postgres database for a Fly application

2

How to retrieve and use database credentials from Heroku

3

How to configure Sequelize to connect to Heroku Postgres

Prerequisites & Requirements

  • Basic understanding of REST APIs and Node.js
  • Familiarity with Heroku and Fly.io(optional)

Key Questions Answered

How can I use Heroku Postgres without migrating my application?
You can use Heroku Postgres with a Fly application by setting up a Heroku account and provisioning a Postgres database as an add-on. This allows you to access Heroku's database services without needing to migrate your application from Heroku to Fly.
What are the steps to retrieve database credentials from Heroku?
To retrieve your database credentials, log in to your Heroku dashboard, navigate to your app's add-ons, select Heroku Postgres, and view the credentials in the settings. The most important credential is the URI, which contains the necessary connection information.
What modifications are needed to connect Sequelize to Heroku Postgres?
You need to parse the DATABASE_URL obtained from Heroku and pass the database, user, password, and host to the Sequelize constructor. Additionally, you must enable SSL in the dialect options to comply with Heroku's requirements.

Key Statistics & Figures

Database capacity on Hobby plan
1GB
This plan can hold up to 10,000 rows, making it suitable for small applications.
Max connections on free plan
20
This limit applies unless you upgrade to a paid plan.

Technologies & Tools

Some links below are affiliate links. We may earn a commission if you make a purchase.

Database
Heroku Postgres
Used as the database service for storing application data.
Orm
Sequelize
Used to connect and interact with the Heroku Postgres database from the Fly application.
Backend
Node.js
Used to build the REST API server that interacts with the database.

Key Actionable Insights

1
Provisioning a Heroku Postgres database on the Hobby plan allows you to use a free database for your Fly application.
This is particularly useful for developers looking to minimize costs while still utilizing a reliable database service.
2
Regularly check your database credentials in Heroku, as they are rotated periodically.
Being proactive about credential management can prevent connection issues in your application, ensuring smooth operation.
3
When configuring Sequelize, ensure that SSL is enabled to connect securely to Heroku Postgres.
This is crucial for maintaining security standards and preventing connection errors due to SSL requirements.

Common Pitfalls

1
Failing to update application credentials after they are rotated by Heroku.
This can lead to connection failures. Always check the credentials in the Heroku dashboard if you encounter issues.
2
Not enabling SSL in Sequelize configuration.
Heroku requires SSL for external connections, and neglecting this setting can result in connection errors.

Related Concepts

Database Management With Heroku
Using Sequelize With Node.js
REST API Development