Migrating Heroku Database Apps To Fly

I was recently asked how the database migration in the original Turboku demo worked. For that original demo, the migration was practically automatic. We already knew that you could access a Heroku database from outside Heroku. Since then, Changes in

Dj Walker-Morgan
6 min readintermediate
--
View Original

Overview

The article discusses the process of migrating database applications from Heroku to Fly, emphasizing the importance of handling database connections effectively. It outlines the steps required to ensure a smooth transition, including the use of SSL for secure connections and the parsing of connection strings.

What You'll Learn

1

How to connect to a Heroku Postgres database from outside Heroku using SSL

2

Why SSL is necessary for secure database connections

3

How to parse connection strings for database connections

4

When to use self-signed certificates in Heroku Postgres connections

Prerequisites & Requirements

  • Basic understanding of PostgreSQL and database connections
  • Familiarity with Node.js and npm packages like Massive.js and pg-connection-string(optional)

Key Questions Answered

How do you migrate a Heroku database to Fly?
To migrate a Heroku database to Fly, you need to ensure that your application can connect to the Heroku Postgres database using SSL. This involves parsing the DATABASE_URL, modifying the SSL settings, and using the parsed connection string to establish a secure connection.
What changes are needed for SSL connections to Heroku Postgres?
When connecting to Heroku Postgres, you must set the SSL field to require SSL and configure rejectUnauthorized to false to bypass the self-signed certificate verification. This ensures a successful connection without errors related to certificate validation.
What is the role of DATABASE_URL in Heroku?
DATABASE_URL is an environment variable created when you attach a Postgres add-on to your Heroku app. It contains all necessary credentials, including the host, port, and database name, allowing your application to connect to the database seamlessly.
How can you test a local Postgres connection?
To test a local Postgres connection, you can omit the DATABASE_URL environment variable. The application will then fall back to a default connection string for a local Postgres instance, allowing for local testing without requiring a Heroku database.

Technologies & Tools

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

Database
Postgresql
Used as the database management system for storing application data.
Backend
Node.js
Used to run the application and manage database connections.
Backend
Massive.js
Used as a data access layer on top of the PostgreSQL database.
Tools
Pg-connection-string
Used to parse PostgreSQL connection strings.

Key Actionable Insights

1
Ensure SSL is enabled when connecting to Heroku Postgres to avoid connection errors.
SSL is mandatory for secure connections to Heroku databases. By configuring SSL correctly, you can prevent issues related to self-signed certificates and maintain secure data transmission.
2
Utilize the pg-connection-string package to simplify connection string parsing.
This package helps extract necessary components from the connection string, making it easier to configure database connections without manual string manipulation.
3
Test your application locally by using a fallback connection string for local Postgres.
This approach allows developers to work on their applications without needing a live Heroku database, facilitating easier development and testing.

Common Pitfalls

1
Failing to configure SSL correctly can lead to connection errors.
Many developers overlook the need for SSL when connecting to Heroku Postgres, which can result in the application failing to connect due to certificate verification issues. Always ensure SSL is properly configured.

Related Concepts

Database Migration
Heroku Postgres
SSL Connections
Node.js Application Development