Skip to main content
Version: v2.x

Postgres: Database to remote database relationships

Introduction

Remote database relationships (a.k.a remote source relationships) extend the concept of joining data between tables within a single database to joining data across tables between separate databases.

After you've established relationships between types in your source database and types in your target database, you can "join" them with GraphQL queries.

Because Hasura is meant to be a GraphQL server that you can expose directly to your apps, Hasura also handles security and authorization while providing remote joins.

Supported from

Remote database relationships for Postgres are supported from versions v2.1.0 and above.

Create remote database relationships

Step 0: Add two database sources

Add a source database as described here and track the required tables. Then, repeat the process to add your target database.

Step 1: Define and create the relationship

A remote database relationship is defined alongside the source database table (that is, the source side of the join).

The following fields can be defined for a remote schema relationship:

  • Relationship type: Either object or array - similar to normal relationships. Hasura supports both many-to-one (object) and one-to-many (array) relationships.
  • Relationship Name: A name for the relationship.
  • Reference Source: The name of the target database (that is, the target side of the join).
  • Reference Table: The table in the target database source that should be joined with the source table
  • Field Mapping: A mapping between fields in the source table and their corresponding fields in the target table, just as a foreign key relationship would be defined by such mapping within a single database.

For example, say we have a table order(id int, user_id int) in the source database and a table user(id int, name text) in the target database.

We can create an object remote database relationship user joining the order table to the user table using the order.user_id and user.id fields.

Head to the Data > [database] > [orders] > Relationships tab. Under the Remote Database Relationships section, select Add a remote database relationship.

Create remote database relationships

Hit Save.

Step 2: Explore with GraphiQL

Run the following query in the GraphiQL editor to test your remote database relationship across the two connected databases:

GraphiQL
Query Variables
Request Headers