Skip Navigation
Show nav
Heroku Dev Center
  • Get Started
  • Documentation
  • Changelog
  • Search
  • Get Started
    • Node.js
    • Ruby on Rails
    • Ruby
    • Python
    • Java
    • PHP
    • Go
    • Scala
    • Clojure
  • Documentation
  • Changelog
  • More
    Additional Resources
    • Home
    • Elements
    • Products
    • Pricing
    • Careers
    • Help
    • Status
    • Events
    • Podcasts
    • Compliance Center
    Heroku Blog

    Heroku Blog

    Find out what's new with Heroku on our blog.

    Visit Blog
  • Log inorSign up
View categories

Categories

  • Heroku Architecture
    • Dynos (app containers)
    • Stacks (operating system images)
    • Networking & DNS
    • Platform Policies
    • Platform Principles
  • Command Line
  • Deployment
    • Deploying with Git
    • Deploying with Docker
    • Deployment Integrations
  • Continuous Delivery
    • Continuous Integration
  • Language Support
    • Node.js
    • Ruby
      • Working with Bundler
      • Rails Support
    • Python
      • Background Jobs in Python
      • Working with Django
    • Java
      • Working with Maven
      • Java Database Operations
      • Working with the Play Framework
      • Working with Spring Boot
      • Java Advanced Topics
    • PHP
    • Go
      • Go Dependency Management
    • Scala
    • Clojure
  • Databases & Data Management
    • Heroku Postgres
      • Postgres Basics
      • Postgres Getting Started
      • Postgres Performance
      • Postgres Data Transfer & Preservation
      • Postgres Availability
      • Postgres Special Topics
    • Heroku Redis
    • Apache Kafka on Heroku
    • Other Data Stores
  • Monitoring & Metrics
    • Logging
  • App Performance
  • Add-ons
    • All Add-ons
  • Collaboration
  • Security
    • App Security
    • Identities & Authentication
    • Compliance
  • Heroku Enterprise
    • Private Spaces
      • Infrastructure Networking
    • Enterprise Accounts
    • Enterprise Teams
    • Heroku Connect (Salesforce sync)
      • Heroku Connect Administration
      • Heroku Connect Reference
      • Heroku Connect Troubleshooting
    • Single Sign-on (SSO)
  • Patterns & Best Practices
  • Extending Heroku
    • Platform API
    • App Webhooks
    • Heroku Labs
    • Building Add-ons
      • Add-on Development Tasks
      • Add-on APIs
      • Add-on Guidelines & Requirements
    • Building CLI Plugins
    • Developing Buildpacks
    • Dev Center
  • Accounts & Billing
  • Troubleshooting & Support
  • Integrating with Salesforce
  • Databases & Data Management
  • Heroku Postgres
  • Postgres Data Transfer & Preservation
  • Heroku Data Links

Heroku Data Links

English — 日本語に切り替える

Last updated December 28, 2021

Table of Contents

  • Overview
  • Listing Data Links
  • Linking Heroku Postgres to Heroku Postgres
  • Linking Heroku Redis to Heroku Postgres
  • Naming a Data Link
  • Removing a Data Link

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.

Keep reading

  • Postgres Data Transfer & Preservation

Feedback

Log in to submit feedback.

SQLite on Heroku Heroku PGBackups

Information & Support

  • Getting Started
  • Documentation
  • Changelog
  • Compliance Center
  • Training & Education
  • Blog
  • Podcasts
  • Support Channels
  • Status

Language Reference

  • Node.js
  • Ruby
  • Java
  • PHP
  • Python
  • Go
  • Scala
  • Clojure

Other Resources

  • Careers
  • Elements
  • Products
  • Pricing

Subscribe to our monthly newsletter

Your email address:

  • RSS
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku Blog
    • Heroku News Blog
    • Heroku Engineering Blog
  • Heroku Podcasts
  • Twitter
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku
    • Heroku Status
  • Facebook
  • Instagram
  • Github
  • LinkedIn
  • YouTube
Heroku is acompany

 © Salesforce.com

  • heroku.com
  • Terms of Service
  • Privacy
  • Cookies
  • Cookie Preferences