Heroku Data Links
Last updated December 28, 2021
Table of Contents
Heroku Data Links helps you connect disparate data sources, such as Heroku Postgres, to a Heroku Postgres database. Once connected, you can use SQL semantics to read and write data, regardless of where the data lives.
Heroku Data Links isn’t available on hobby tier databases.
Overview
Heroku Data Links uses a native feature of Postgres called Foreign Data Wrappers. A Foreign Data Wrapper maps the logical representation of foreign tables on a foreign server to tables in the local database.
When you write a query against data in a remote database, Postgres transparently queries the remote data. The details of where the data lives and how it’s abstracted in Postgres are pushed down into the Postgres extension that connects the two data stores.
Heroku Data Links takes care of the difficult parts of managing Foreign Data Wrappers, including creating the server specification within the local database and creating tables to query against in the remote system.
Terminology
The documentation on Heroku Data Links uses a strict set of terms to define all of the interactions.
- Remote - The data store that is being connected to a Heroku Postgres database.
- Local - The Heroku Postgres database that’s accepting the connection.
- Data Link - The name of connection between the remote and local databases.
Listing Data Links
All of the Data Links that have been created can be listed via the Heroku CLI:
$ heroku pg:links -a example-app
=== postgresql-animate-91581
* datalink_test
created_at: 2021-10-11T14:46:22.721+00:00
remote: HEROKU_POSTGRESQL_BLUE_URL (postgresql-colorful-33594)
remote_name: postgresql_colorful_33594
=== postgresql-colorful-33594
No data sources are linked into this database.
=== postgresql-round-06985
This operation is not supported by hobby-dev databases
The command lists all of the databases attached to an application and existing links within each database. In the above example, DATABASE_URL
is the local database, also known by its resource add-on name warming-calmly-5763
.
The Remote:
line references the config var and the resource name of the database that’s connected to DATABASE_URL
. Remote Name
is the server reference inside of the DATABASE_URL
database.
Linking Heroku Postgres to Heroku Postgres
You can also link Heroku Postgres to another Heroku Postgres database:
$ heroku pg:links:create HEROKU_POSTGRESQL_BLUE HEROKU_POSTGRESQL_RED --as datalink_test -a example-app
When Heroku Postgres instances are linked, a new schema is added to Postgres based on the link name (in this case, datalink_test
schema). If you have the people
table in the remote data store HEROKU_POSTGRESQL_BLUE
, you can query with that table like this, from the HEROKU_POSTGRESQL_RED
database:
SELECT * FROM datalink_test.people;
Only the tables that exist at the time of the link creation are available. If you create tables after you created the link, they won’t show up in the local database. If you want to include newly created tables, you must remove the link and create it again.
When you delete the remote data store, or the remote date store moves to another host (for example, Postgres failover), the data link won’t be updated. Also, when you reset your local database with the command like heroku pg:reset
, the data link information is also reset. You must recreate the data link for these cases.
Linking Heroku Redis to Heroku Postgres
Creation of Heroku Data Links from Heroku Redis to Heroku Postgres has been deprecated as of January 30, 2018.
Naming a Data Link
A good practice when creating links is to give it a meaningful name. Use the --as
flag to specify a link name:
$ heroku pg:links:create HEROKU_POSTGRESQL_BLUE HEROKU_POSTGRESQL_RED --as datalink_test -a example-app
Names must consist of lower case alphanumeric characters and underscores and be at most 63 characters.
When a Data Link is named, query the remote Postgres table using the same name in the --as
flag:
SELECT id, name, description FROM datalink_test.remote_table_name;
Removing a Data Link
To remove your Data Link via the CLI:
$ heroku pg:links:destroy HEROKU_POSTGRESQL_BLUE datalink_test -a example-app
! WARNING: Destructive Action
! This command will affect the database: DATABASE
! This will delete datalink_test along with the tables and views created within it.
! This may have adverse effects for software written against the datalink_test schema.
! To proceed, type "example-app" or re-run this command with --confirm example-app
> example-app
Deleting link datalink_test in DATABASE... done
If any views or code have been written against a Postgres table that comes from a Data Link, no checks are made before the Data Link is deleted.