AWS, Cloud Computing, Data Analytics

3 Mins Read

A Guide to SetUp a Foreign Server in Amazon RDS for PostgreSQL

Voiced by Amazon Polly

Introduction

Amazon RDS for PostgreSQL is a managed database service that allows seamless integration with external databases using Foreign Data Wrappers (FDW). FDWs enable PostgreSQL to access data from other databases (PostgreSQL or other sources) as local tables.

Pioneers in Cloud Consulting & Migration Services

  • Reduced infrastructural costs
  • Accelerated application deployment
Get Started

Understanding Foreign Data Wrappers (FDW)

FDWs in PostgreSQL allow database administrators to query external databases. They provide an abstraction layer, enabling remote data access with minimal performance overhead. Some common use cases include:

  • Accessing a remote PostgreSQL database
  • Integrating Amazon RDS with on-premise databases
  • Querying heterogeneous data sources (e.g., MySQL, Oracle, MongoDB, Redshift)

PostgreSQL supports multiple FDWs, including:

  • postgres_fdw (for PostgreSQL-to-PostgreSQL connections)
  • mysql_fdw (for MySQL integration)
  • odbc_fdw (for ODBC-compatible databases)
  • For this tutorial, we will focus on postgres_fdw to connect two PostgreSQL databases, both hosted on Amazon RDS.

Prerequisites

Before configuring a foreign server in Amazon RDS for PostgreSQL, ensure the following:

  • Amazon RDS PostgreSQL Instance is up and running.
  • Extensions (postgres_fdw) are supported in your PostgreSQL version.
  • Security Groups allow connections between the RDS instances.
  • A database user with the required privileges is available.

You can check your PostgreSQL version by running:

Step-by-Step Guide

Step 1: Enable the postgres_fdw Extension

First, enable the postgres_fdw extension on the primary database:

Verify the installation:

Step 2: Create a Foreign Server

A foreign server represents the remote PostgreSQL database. Use the following syntax:

Replace:

  • <REMOTE_DB_ENDPOINT> with the remote RDS PostgreSQL instance endpoint
  • <REMOTE_DB_NAME> with the target database name

To list foreign servers:

Step 3: Create a User Mapping

A user mapping links local users to remote database users. Create it using:

Check existing mappings:

For security, avoid storing credentials in SQL scripts and use AWS Secrets Manager instead.

Step 4: Create a Foreign Table

A foreign table acts as a proxy to the remote table. Define it as follows:

The columns must match the remote table’s structure.

The schema_name should be the actual schema in the remote database.

To verify:

Step 5: Querying the Remote Table

Once the foreign table is set up, query it as a local table:

To ensure performance, analyze query execution:

Step 6: Managing and Securing Foreign Servers

Granting Privileges

Grant access to specific users:

Updating User Mapping

Modify existing credentials:

Dropping Foreign Server or Table

To remove the foreign table:

To drop the foreign server:

Best Practices for Foreign Data Wrappers

Optimize Performance:

  • Use LIMIT when querying large datasets.
  • Avoid complex joins with foreign tables.
  • Consider materialized views for frequently accessed data.

Secure Connections:

  • Use SSL to encrypt connections between databases.
  • Store credentials securely (AWS Secrets Manager, IAM).

Monitor Query Performance:

  • Use pg_stat_activity to monitor FDW-related queries:

Use Connection Pooling

  • For frequent queries, consider PgBouncer to manage persistent connections.

Advantages of Using Foreign Servers in Amazon RDS PostgreSQL

  1. Seamless Data Integration
    • FDWs allow PostgreSQL to query remote databases as if they were local, reducing the need for complex ETL (Extract, Transform, Load) processes.
  2. Real-Time Access to Remote Data
    • Instead of duplicating data, you can fetch live records from remote databases, ensuring data consistency across multiple environments.
  3. Reduced Storage Costs
    • Since FDWs query external databases instead of storing duplicate data, they help save on Amazon RDS storage costs.
  4. Cross-Region and Cross-Account Data Access
    • Foreign servers enable secure data access between PostgreSQL instances across different AWS accounts or regions.

Conclusion

Setting up a foreign server in Amazon RDS PostgreSQL enables seamless data integration between databases. You can efficiently access and manage remote data by following the steps outlined enabling postgres_fdw, creating a foreign server, mapping users, defining foreign tables, and securing connections.

Drop a query if you have any questions regarding Amazon RDS PostgreSQL and we will get back to you quickly.

Empowering organizations to become ‘data driven’ enterprises with our Cloud experts.

  • Reduced infrastructure costs
  • Timely data-driven decisions
Get Started

About CloudThat

CloudThat is a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.

CloudThat is the first Indian Company to win the prestigious Microsoft Partner 2024 Award and is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 650k+ professionals in 500+ cloud certifications and completed 300+ consulting projects globally, CloudThat is an official AWS Advanced Consulting Partner, Microsoft Gold Partner, AWS Training PartnerAWS Migration PartnerAWS Data and Analytics PartnerAWS DevOps Competency PartnerAWS GenAI Competency PartnerAmazon QuickSight Service Delivery PartnerAmazon EKS Service Delivery Partner AWS Microsoft Workload PartnersAmazon EC2 Service Delivery PartnerAmazon ECS Service Delivery PartnerAWS Glue Service Delivery PartnerAmazon Redshift Service Delivery PartnerAWS Control Tower Service Delivery PartnerAWS WAF Service Delivery PartnerAmazon CloudFrontAmazon OpenSearchAWS DMSAWS Systems ManagerAmazon RDS, and many more.

FAQs

1. Can I use a foreign server to connect an Amazon RDS PostgreSQL instance with an on-premise PostgreSQL database?

ANS: – Yes, you can use postgres_fdw to connect your RDS PostgreSQL instance with an on-premise PostgreSQL database. However, you need to:

  • Ensure network connectivity between your on-premise environment and RDS.
  • Configure VPC Peering, AWS VPN, or AWS Direct Connect for secure access.
  • Modify the security groups and inbound rules to allow connections from RDS to the on-premise database.

2. How can I improve the performance of queries using Foreign Data Wrappers (FDW)?

ANS: – To optimize FDW performance:

  • Use LIMIT when querying large remote tables to reduce data transfer.
  • Leverage EXPLAIN ANALYZE to check how queries are executed.
  • Enable query pushdown by filtering data before fetching it from the remote database.
  • Use Materialized Views to store frequently accessed remote data locally for faster performance.
  • Consider connection pooling with PgBouncer to manage multiple FDW connections efficiently.

WRITTEN BY Sunil H G

Sunil H G is a highly skilled and motivated Research Associate at CloudThat. He is an expert in working with popular data analysis and visualization libraries such as Pandas, Numpy, Matplotlib, and Seaborn. He has a strong background in data science and can effectively communicate complex data insights to both technical and non-technical audiences. Sunil's dedication to continuous learning, problem-solving skills, and passion for data-driven solutions make him a valuable asset to any team.

Share

Comments

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!