Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

Your laptop cannot reach an RDS database, and ping does not work either

ErrorCould not connect to server: Connection timed out

Short answer

When a client times out against an RDS endpoint, the network path is blocked, not the database login. Test the port with nc first. Then check four things in order: the security group's inbound rule for your IP, the instance's Public access setting, the subnet's route to an internet gateway, and a local firewall or busy local port. Ping never works on RDS.

  • Applies to Amazon RDS for MySQL 8.4, Amazon RDS for PostgreSQL 17, Linux or macOS client with nc
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Test the TCP port instead of using ping. Amazon RDS does not accept ICMP traffic, so a failed ping tells you nothing. Replace the endpoint and port (3306 for MySQL, 5432 for PostgreSQL) with your own. A success message means the network path is open and the problem is the login; a hang or failure means one of the causes below.

    nc -zv mydb.xxxx.us-east-1.rds.amazonaws.com 3306
  2. 2Match what you see, then jump to its fix

    1. 1nc hangs and then fails, while the instance status is availableThe security group has no inbound rule for your addressFix
    2. 2The console shows Public access: No for the instanceThe instance is not publicly accessibleFix
    3. 3Public access is Yes and the security group is open, but the connection still times outThe subnets have no route to an internet gatewayFix
    4. 4Other sites work, the RDS rules look right, and the test only fails from this networkYour own network blocks the port, or the port is taken locallyFix

What does the error look like?

  • Could not connect to server: Connection timed out
  • Ping to the RDS endpoint gets no reply

Cause 1 of 4

The security group has no inbound rule for your address

1Check

RDS instances allow no access by default. Access comes from the security group attached to the instance, and a rule must allow your current public IP on the database port. Look at the group in the instance's Connectivity & security tab.

aws rds describe-db-instances --db-instance-identifier mydb \
  --query "DBInstances[0].VpcSecurityGroups[].VpcSecurityGroupId"

2Fix

Add an inbound rule for the database port. In the console choose My IP as the source, which uses the address your browser shows. Note that connections from 169.254.0.0/16 are never allowed.

aws ec2 authorize-security-group-ingress --group-id sg-0123456789abcdef0 \
  --protocol tcp --port 3306 --cidr 203.0.113.25/32

3Verify

Run the port test again.

nc -zv mydb.xxxx.us-east-1.rds.amazonaws.com 3306

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 2 of 4

The instance is not publicly accessible

1Check

To reach an instance from outside its VPC, for example from your own computer, it needs a public IP address. That is the Public access setting.

aws rds describe-db-instances --db-instance-identifier mydb \
  --query "DBInstances[0].PubliclyAccessible"

2Fix

Modify the instance and set Public access to Yes. If you would rather keep the database private, connect from inside the VPC instead, for example from an EC2 instance in the same VPC.

aws rds modify-db-instance --db-instance-identifier mydb \
  --publicly-accessible --apply-immediately

3Verify

Wait until the instance status is available, then repeat the port test.

nc -zv mydb.xxxx.us-east-1.rds.amazonaws.com 3306

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 3 of 4

The subnets have no route to an internet gateway

1Check

A publicly accessible instance needs the subnets of its DB subnet group to have an internet gateway. In the VPC console, open the subnet's route table and look for a route with destination 0.0.0.0/0 whose target is the internet gateway (and ::/0 as well if you connect over IPv6).

# VPC console > Subnets > pick the subnet > Route table tab: is 0.0.0.0/0 sent to an igw-... target?

2Fix

Attach an internet gateway to the VPC if there is none, then add the 0.0.0.0/0 route (and ::/0 for IPv6) pointing at it in the route table of each subnet in the DB subnet group.

aws ec2 create-route --route-table-id rtb-0123456789abcdef0 \
  --destination-cidr-block 0.0.0.0/0 --gateway-id igw-0123456789abcdef0

3Verify

Repeat the port test from your machine.

nc -zv mydb.xxxx.us-east-1.rds.amazonaws.com 3306

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 4 of 4

Your own network blocks the port, or the port is taken locally

1Check

A company or home firewall can block outbound traffic to the database port. For PostgreSQL the default 5432 can also be occupied on your computer by a local PostgreSQL server, so the client talks to that one instead. Try the same test from a different network, and see what listens locally.

nc -zv localhost 5432

2Fix

Ask the network administrator to allow the port, or modify the instance to use a different database port (and open that port in the security group). Stop or move the local service if it holds the same port.

aws rds modify-db-instance --db-instance-identifier mydb \
  --db-port-number 5433 --apply-immediately

3Verify

Test the new port from the affected network.

nc -zv mydb.xxxx.us-east-1.rds.amazonaws.com 5433

Confirmed by docs.aws.amazon.com

Did this fix it?

How do I stop it coming back?

  • Use the instance endpoint name, not an IP address, in every connection string.
  • Keep database access in a security group you name and document; avoid 0.0.0.0/0 on the database port.
  • Test with nc -zv, never with ping, when you check RDS reachability.
  • Prefer a private database reached from inside the VPC for production, and open Public access only for short work.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.