I'm using AWS RDS and I want to connect to the database remotely. However, I keep getting
Database connection failed due to connection to server at
"mydb.12345.eu-central-1.rds.amazonaws.com" (x.xx.xxx.xx),
port 5432 failed: Connection timed out (0x0000274C/10060)
Is the server running on that host and accepting TCP/IP connections?
These seems to be a very common problem and all the solutions suggest inbound rules should be set to accept all traffic from you own IP.
However, for me this doesn't solve the issue.
This is my setup:
These are the inbound rules in security group sg-650cbe0b
I have also tried adding inbound rules such as:
or
But it didn't work.
I have tried connecting via my mobile network (to see if it's a firewall issue), but I got the same error.
However, I access this database from within a AWS Lambda function, and that works without problems.
This is the code I'm using to access the database:
import psycopg2
import sys
import boto3
import os
ENDPOINT="mydb.12345.eu-central-1.rds.amazonaws.com"
PORT="5432"
USER="admin"
REGION="eu-central-1b"
DBNAME="mydb"
#gets the credentials from .aws/credentials
session = boto3.Session()
client = session.client('rds')
token = client.generate_db_auth_token(DBHostname=ENDPOINT, Port=PORT, DBUsername=USER, Region=REGION)
try:
conn = psycopg2.connect(host=ENDPOINT, port=PORT, database=DBNAME, user=USER, password=token, sslrootcert="SSLCERTIFICATE")
cur = conn.cursor()
cur.execute("""SELECT now()""")
query_results = cur.fetchall()
print(query_results)
except Exception as e:
print("Database connection failed due to {}".format(e))
CodePudding user response:
There are a number of reasons that RDS connectivity could fail, including:
- the RDS instance was not configured to be publicly accessible
- it was launched into a private subnet and has no route to an IGW
I'd recommend the RDS connectivity troubleshooter: How can I troubleshoot connectivity to an Amazon RDS DB instance that uses a public or private subnet of a VPC?