Amazon RDS PostgreSQL“ psql:错误:无法连接到服务器:无法连接到服务器:操作超时”

问题描述

我不知道如何连接到我的Amazon PostgreSQL数据库实例。我确定我做错了什么,其他Stack Overflow线程都无济于事。这是同一VPC中的EC2实例访问的VPC中的RDS数据库实例。这就是我所做的。

创建了一个自定义EC2 VPC安全组,并指定了我的默认VPC

  • aws ec2 create-security-group --group-name rds-us-east-databases --vpc-id vpc-1234567 --description "RDS databases in the us-east-1 region"

为我的默认子网创建RDS数据库子网组

  • aws rds create-db-subnet-group --db-subnet-group-name rds-us-east-databases --db-subnet-group-description "RDS databases" --subnet-ids '["subnet-97xxx9","subnet-e1xxxxdf","subnet-a62xxx9","subnet-baxxxxdc","subnet-axxxxe2","subnet-11xxxx0"]'

为我的EC2 VPC安全组添加了CIDR流量规则 **:

  • aws ec2 authorize-security-group-ingress --group-name rds-us-east-databases --protocol tcp --port 5432 --cidr 160.3.6.253/32
  • aws ec2 authorize-security-group-ingress --group-name rds-us-east-databases --protocol tcp --port 5432 --cidr 192.168.1.0/24
  • aws ec2 authorize-security-group-ingress --group-name rds-us-east-databases --protocol tcp --port 5432 --cidr 10.0.0.0/16

**我在这里测试流量规则。

创建了我的PostgreSQL数据库并指定了EC2 VPC安全组ID

  • aws rds create-db-instance --db-name DBName --db-instance-identifier MyDbNameID --allocated-storage 20 --db-instance-class db.t2.micro --engine Postgres --master-username postgres --master-user-password MyDatabasePassword1 --vpc-security-group-ids '["sg-02xxxxxxxxxxxx90c"]' --db-subnet-group-name rds-us-east-databases --backup-retention-period 3 --port 5432 --no-publicly-accessible --enable-iam-database-authentication --region us-east-1 --max-allocated-storage 100

试图使用psql连接

psql --host=blah.blah.us-east-1.rds.amazonaws.com --port=5432 --username=postgres --password --dbname=DBName

错误

psql: error: could not connect to server: could not connect to server: Operation timed out
    Is the server running on host “blah.blah.rds.amazonaws.com" (IP address) and accepting
    TCP/IP connections on port 5432?

注意

  • 以上所有功能均在同一VPC上/使用。
  • 几个月前,我在另一个RDS PostgreSQL数据库上遇到了相同的问题,并且能够连接。虽然,我不记得我做了什么修复。因此,故障不是默认的VPC造成的。
  • 还确认我在postgresql.conf中具有远程访问设置,并且由于我可以连接到其他RDS数据库,因此似乎不是本地PostgreSQL问题。
  • 还确认它与公众可访问性没有任何关系。尝试使其可以公开访问,并遇到致命错误,请切换回。

与RDS DB的其他成功连接如下所示:

psql --host=database-2.otherdbendpoint.us-east-1.rds.amazonaws.com --port=5432 --username=postgres --password --dbname=myotherdatabase
Password: 
psql (12.3,server 11.6)
SSL connection (protocol: TLSv1.2,cipher: ECDHE-RSA-AES256-GCM-SHA384,bits: 256,compression: off)
Type "help" for help.
myotherdatabase=>

更新:我的VPC安全组'[“ sg-02xxxxxxxxxxxx90c”]'的所有入口规则:

PostgreSQL  TCP 5432    111.1.1.111/32  MY IP ADDRESS
PostgreSQL  TCP 5432    192.168.1.0/24  -
PostgreSQL  TCP 5432    0.0.0.0/0   -
PostgreSQL  TCP 5432    10.0.0.0/16 -
PostgreSQL  TCP 5432    10.0.0.0/24 -
All traffic All All     5432::/16   -

更新:我对VPC安全组'[“ sg-02xxxxxxxxxxxx90c”]'的出口规则:

All traffic All All 0.0.0.0/0   -

解决方法

您似乎已确认 RDS 实例的安全组允许来自任何 IP 的相应端口上的流量。

检查与您的实例关联的子网,并确保路由表将公共流量定向到互联网网关。

0.0.0.0/16 -> igw-xxxxxxxxxxxxx

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...