pail
pail

Reputation: 311

postgres_fdw cannot connect to server on Amazon RDS

I have two Postgres 9.3.5 instances in RDS, both in one security group that allows all inbound traffic from within the security group and all outbound traffic. I'm trying to set up one database to be able to select from a few tables from the other via postgres_fdw.

I've created the server -

create server master 
foreign data wrapper postgres_fdw 
OPTIONS (dbname 'main', 
         host 'myinstance.xxxxx.amazonaws.com');

as well as the requisite user mapping and foreign table -

create foreign table condition_fdw (
    cond_id integer,
    cond_name text
) server master options(table_name 'condition', schema_name 'data');

However, a simple select count(*) from condition_fdw gives me

ERROR:  could not connect to server "master"
DETAIL:  could not connect to server: Connection timed out
        Is the server running on host "myinstance.xxxxxx.amazonaws.com" (xx.xx.xx.xx) and accepting
        TCP/IP connections on port 5432?

I can connect to both databases via psql from an EC2 instance. I know until recently RDS didn't support postgres_fdw, but I'm running the newer versions that do.

In the create server statement, I have tried replacing "myinstance.xxxxxx.amazonaws.com" with the IP address it resolves to, no luck.

Any ideas?

Further Testing

I installed postgres on an ec2 instance with the same security group, foreign tables to the master server behave as expected.

postgres_fdw between databases on the same RDS instance works.

This all leads me to think it must some issue with outgoing connections from postgres_fdw on my Postgres RDS instance.

Upvotes: 7

Views: 7133

Answers (4)

bheden
bheden

Reputation: 31

To get postgres_fdw to work between two instances in AWS RDS (with VPC) I had to:

  1. Enable custom_dns_resolution https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Appendix.PostgreSQL.CommonDBATasks.html#Appendix.PostgreSQL.CommonDBATasks.CustomDNS
  2. Add the public IP of the querying database in the inbound rules of the Security group OR add the internal IP of the RDS instance as server host. https://forums.aws.amazon.com/thread.jspa?threadID=235154

The first one is documented pretty well but I could not get it to work until stumbling over the second one.

Upvotes: 3

Tisha
Tisha

Reputation: 857

If you are in the same availability zone, there is no reason why it wouldn't work.

Upvotes: 0

James King
James King

Reputation: 1

I experienced a similar problem and found that postgres_fdw would only work in the EU-WEST-1 RDS region when the master and remote instances were in the same availability zone and on 9.3.5, if you crossed AZs it wouldn't connect. My security group was 5432 TCP inbound only and all outbound.

Upvotes: 0

pail
pail

Reputation: 311

It would appear that Amazon does not allow outgoing connections from RDS instances, so until that changes using postgres_fdw across RDS instances is not possible. I'll have to run an ec2 instance as my postgres server in order to use a foreign table to a database on another server.

http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Overview.RDSSecurityGroups.html

Upvotes: 0

Related Questions