This topic describes how an RDS for PostgreSQL instance can use the foreign data wrapper (FDW) extension to access an external database over the internet.
Background
RDS for PostgreSQL instances run in a Virtual Private Cloud (VPC) and require an Internet NAT Gateway with an associated elastic IP address (EIP) to access database services over the internet.
This configuration enables your RDS for PostgreSQL instance to access the internet while ensuring its network security. With SNAT, the instance can access the internet but remains inaccessible from it through the NAT gateway.
For more information about Internet NAT Gateway and SNAT, see Use the SNAT feature of an Internet NAT Gateway to access the internet.
Prerequisites
-
A target database, such as MySQL, SQL Server, PostgreSQL, or Redis, that is accessible over the internet via a public IP address.
-
You have an RDS for PostgreSQL instance. For instructions, see Quickly create an RDS for PostgreSQL instance.
ImportantTo access an internet-facing target database from an RDS for PostgreSQL instance, you must use a specific extension, as listed below. Ensure that your instance supports the required extension. For information about which extensions are supported by each version, see Extensions supported by RDS for PostgreSQL.
-
MySQL: mysql_fdw
-
SQL Server: tds_fdw
-
PostgreSQL: postgres_fdw
-
Redis: redis_fdw
-
-
You have created accounts on both the internet-facing target database and the RDS for PostgreSQL instance.
-
To create an account for the internet-facing target database, see the official documentation for the respective database engine.
-
To create an account for your RDS for PostgreSQL instance, see Create an account.
-
-
The internet-facing target database has a database that contains data.
Procedure
Configure an Internet NAT Gateway
-
Create an Internet NAT Gateway.
-
Log on to the NAT Gateway console.
-
On the Internet NAT Gateway page, click Create NAT Gateway.
-
(Optional) If this is your first time using NAT Gateway, on the Internet NAT Gateway page, find the Service-linked Role Creation section and click Create Service-Linked Role. After the role is created, you can create the NAT gateway.
-
On the Create Internet NAT Gateway page, configure the following parameters and click Buy Now.
NoteThis table lists only the key parameters. For detailed information about all parameters, see Use the SNAT feature of an Internet NAT Gateway to access the internet.
Parameter
Description
Region
Select the region of your RDS for PostgreSQL instance.
Network and Zone
Select the VPC and vSwitch that are used by your RDS for PostgreSQL instance.
You can find the VPC of the target RDS for PostgreSQL instance on the Database Connection page in the ApsaraDB RDS console.
Network Type
Select Internet NAT Gateway.
EIP
In this example, select Configure Later.
-
On the Confirm Order page, verify the configuration information for the Internet NAT Gateway and click Activate Now.
After the gateway is created, you can view the instance on the Internet NAT Gateway page. The instance status is Pending Configuration and the specification/type is Enhanced.
-
-
Associate an elastic IP address (EIP) with the Internet NAT Gateway.
-
In the NAT Gateway console, find the new Internet NAT Gateway instance and click its ID to open the details page.
-
Go to the Associated EIP tab and click Associate EIP.
-
In the Associate EIP dialog box, select Purchase and Associate EIP.
-
Click OK.
After the EIP is associated, you can view it on the Associated EIP tab. Its status is Allocated, and the Entry Association Status is Unused. This indicates that the EIP is not yet associated with a DNAT or SNAT entry.
-
-
Create an SNAT entry.
-
In the NAT Gateway console, find the new Internet NAT Gateway instance and click its ID to go to the details page.
-
Go to the SNAT tab and click Create SNAT Entry.
-
On the Create SNAT Entry page, configure the following parameters and click OK.
Parameter
Description
SNAT Entry
Select the granularity for the SNAT entry. This example uses Select vSwitch. This setting allows the RDS for PostgreSQL instance in the specified vSwitch to access the internet using the public IP address.
Select vSwitch
Select the vSwitch of your RDS for PostgreSQL instance.
Select EIP
Select the public IP address to use for internet access. This example uses a single IP address. Select the associated EIP from the drop-down list.
After the entry is created, you can view it in the SNAT Entry List. On the SNAT Management tab, the status of the new SNAT entry is Available.
-
Configure the internet-facing target database
Add the EIP of the Internet NAT Gateway to the whitelist of the target database.
-
MySQL: See Privileges Provided by MySQL
-
PostgreSQL: See The pg_hba.conf File
-
SQL Server: See Configure the Windows Firewall to Allow SQL Server Access
-
Redis: Use a firewall to restrict access to the specified port by IP address. For example, if you use iptables on CentOS, run the following command:
iptables -A INPUT -s <EIP_of_your_NAT_gateway> -p tcp --dport <Redis_port> -j ACCEPT
Configure the RDS for PostgreSQL instance
-
Connect to the RDS for PostgreSQL instance. For instructions, see Connect to a PostgreSQL instance.
-
Create the extension.
NoteThis example uses an internet-facing MySQL database as the target and creates the mysql_fdw extension.
CREATE EXTENSION mysql_fdw; -
Create a server definition for the internet-facing target database.
CREATE SERVER <server_name> FOREIGN DATA WRAPPER mysql_fdw OPTIONS ( host '<public_IP_of_target_database>', port '<port_of_target_database>' );The following is an example for an internet-facing MySQL service:
CREATE SERVER mysql_server80 FOREIGN DATA WRAPPER mysql_fdw OPTIONS ( host 'XX.XX.XX.XX', port '3306' ); -
Create a user mapping. This mapping allows the specified RDS for PostgreSQL user to access data from the target database.
CREATE USER MAPPING FOR <RDS_for_PostgreSQL_username> SERVER <created_server_name> OPTIONS ( username '<username_for_target_database>', password '<password_for_target_database>' );Example:
CREATE USER MAPPING FOR pg_client SERVER mysql_server80 OPTIONS ( username 'testuser', password 'U123456!' ); -
Create a foreign table.
CREATE FOREIGN TABLE <foreign_table_name> ( id int, name varchar(10) ) SERVER <created_server_name> OPTIONS ( dbname '<database_name_on_target_database>', table_name '<table_name_on_target_database>' );Example:
CREATE FOREIGN TABLE mysql_fdw_test ( id int, name varchar(10) ) SERVER mysql_server80 OPTIONS ( dbname 'testdb', table_name 'test' ); -
Test the connection.
After you complete these steps, you can access the tables in the internet-facing database from your RDS for PostgreSQL instance.
For example, if you create a foreign table named
mysql_fdw_test, you can query it in your RDS for PostgreSQL instance to retrieve data from the internet-facing target database.SELECT * FROM mysql_fdw_test;