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:
2. Create a Foreign Server Connection
Set up a connection to your second PostgreSQL database:
3. Create a User Mapping
Map your local database user to the remote database credentials:
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
Security Groups: Ensure the security groups for both RDS instances allow traffic between them
VPC Configuration: Both databases should be in the same VPC or have VPC peering configured
IAM Permissions: If using IAM authentication, ensure proper permissions are set
Network Performance: Cross-database queries may have higher latency, so materialized views are often preferable for frequent access patterns
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?