Join tables separate PostgreSQL databases

When we have two different PostgreSQL databases in AWS, you can join tables from these separate databases using PostgreSQL's Foreign Data Wrapper (FDW) functionality to create views or materialized views. This is an excellent approach for your scenario.

Here's how to set it up:

1. Enable the PostgreSQL Foreign Data Wrapper

First, you need to enable the postgres_fdw extension in your primary PostgreSQL database:

CREATE EXTENSION postgres_fdw;

2. Create a Foreign Server Connection

Set up a connection to your second PostgreSQL database:

CREATE SERVER foreign_server
FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (host 'second-db-hostname.region.rds.amazonaws.com', port '5432', dbname 'second_database');

3. Create a User Mapping

Map your local database user to the remote database credentials:

CREATE USER MAPPING FOR current_user
SERVER foreign_server
OPTIONS (user 'remote_username', password 'remote_password');

4. Create Foreign Tables

Create foreign tables that reference the tables in your second database:

5. Create a View Joining Local and Foreign Tables

Now you can create a view that joins local tables with the foreign tables:

6. Create a Materialized View (for performance)

For better performance, especially with larger datasets, you can create a materialized view:

You'll need to refresh the materialized view when data changes:

AWS-Specific Considerations

  1. Security Groups: Ensure the security groups for both RDS instances allow traffic between them

  2. VPC Configuration: Both databases should be in the same VPC or have VPC peering configured

  3. IAM Permissions: If using IAM authentication, ensure proper permissions are set

  4. Network Performance: Cross-database queries may have higher latency, so materialized views are often preferable for frequent access patterns

  5. RDS Parameter Groups: You might need to modify parameter groups to enable FDW if it's not enabled by default

Using this approach, you can effectively join tables from two different PostgreSQL databases in AWS without the limitations of DMS for cross-database joins.

Last updated

Was this helpful?