我想通过本地的 EC2 连接到 AWS RDS。
我在本地计算机上创建了一个隧道:
ssh -i ~/.ssh/id_rsa -f -N -L 5432:RDS-Endpoint:5432 EC2-User@EC2-IP -v
这个方法可行:
psql -hlocalhost -Upostgres -p5432 -d postgres
它确实连接到了RDS数据库。
但是如果使用另一个经过 SSL 认证的 postgres 用户
psql -hlocalhost -p5432 "sslmode=verify-full sslrootcert=rds-ca-2019-root.pem user=another_user dbname=my_db"
导致此错误:
psql: error: server certificate for "tf-xxx.rds.amazonaws.com" (and 2 other names) does not match host name "localhost"
如果在EC2服务器上执行,效果很好。在本地,似乎证书不匹配
localhost
。那么如何正确设置呢?
我认为您可以通过将“hostaddr”指定为 127.0.0.1,但将“host”指定为真实主机名来实现此目的。
在 PostgreSQL 中使用 SSL 加密和
sslmode=verify-full
与使用 SSL 隧道是互斥的,因为端点实际上是“中间人”。
我在 AWS RDS (postgres 16) 上解决此问题的方法是设置一个参数组。将参数组分配给选择的数据库。您可以创建新的参数组或编辑现有的参数组。
在可修改参数的搜索框中,搜索 ssl 并单击 rds.force_ssl。将值更改为 0。单击“保存更改”以保存更改。 现在,转到 AWS RDS 仪表板上的数据库并修改数据库
在数据库的附加配置部分,将新创建的参数组(从下拉列表中选择)添加到数据库中。确保保存必要的更改。
N:B: 还要确保您的数据库是公开可用的或允许从您的 IP 访问。
按照此方法,您应该能够访问 AWS 上托管的 postgres 数据库。但是,请注意,这可能会存在一些安全问题,因为它仅建议用于测试目的。