OperationalError:无法连接到服务器:操作超时 - 尝试将数据库从 sqlite 更改为 postresql 时

OperationalError:无法连接到服务器:操作超时 - 尝试将数据库从 sqlite 更改为 postresql 时

我已将我的 RDS amazon db 实例 (postgresql 数据库) 从微型升级到小型。尽管它说它在亚马逊控制台上可用,但(psycopg2.OperationalError) could not connect to server: Connection timed out每次我从我的应用程序访问数据库时,我都会不断收到错误。当我尝试使用以下命令从我的 ubuntu 机器访问服务器时:

psql -h Ip -d db -U myuser

我犯了同样的错误:

psql: could not connect to server: Connection timed out
    Is the server running on host "IP" and accepting
    TCP/IP connections on port 5432?

另一方面,PgAdmin4 内部一切似乎也都很好。

我想知道我是否只需要等待一段时间让一切初始化。顺便问一下,我正在开发一个 Django 项目,在这样的升级之后我需要注意哪些设置?有人遇到过同样的问题吗?

这里是 postgres 错误日志:

2020-08-07 10:00:16 UTC::@:[13371]:LOG: checkpoint starting: time
2020-08-07 10:00:16 UTC::@:[13371]:LOG: checkpoint complete: wrote 1 buffers (0.0%); 0 WAL file(s) added, 0 removed, 1 recycled; write=0.101 s, sync=0.005 s, total=0.132 s; sync files=1, longest=0.005 s, average=0.005 s; distance=65536 kB, estimate=65537 kB
2020-08-07 10:05:16 UTC::@:[13371]:LOG: checkpoint starting: time
2020-08-07 10:05:16 UTC::@:[13371]:LOG: checkpoint complete: wrote 1 buffers (0.0%); 0 WAL file(s) added, 0 removed, 1 recycled; write=0.101 s, sync=0.005 s, total=0.136 s; sync files=1, longest=0.005 s, average=0.005 s; distance=65536 kB, estimate=65537 kB
2020-08-07 10:10:16 UTC::@:[13371]:LOG: checkpoint starting: time
2020-08-07 10:10:16 UTC::@:[13371]:LOG: checkpoint complete: wrote 1 buffers (0.0%); 0 WAL file(s) added, 0 removed, 1 recycled; write=0.101 s, sync=0.005 s, total=0.135 s; sync files=1, longest=0.005 s, average=0.005 s; distance=65534 kB, estimate=65537 kB
2020-08-07 10:12:03 UTC::@:[13368]:LOG: received fast shutdown request
2020-08-07 10:12:03 UTC::@:[13368]:LOG: aborting any active transactions
2020-08-07 10:12:03 UTC::@:[13368]:LOG: background worker "logical replication launcher" (PID 13377) exited with exit code 1
2020-08-07 10:12:03 UTC::@:[13371]:LOG: shutting down
2020-08-07 10:12:03 UTC::@:[13371]:LOG: checkpoint starting: shutdown immediate
2020-08-07 10:12:03 UTC::@:[13371]:LOG: checkpoint complete: wrote 1 buffers (0.0%); 0 WAL file(s) added, 0 removed, 1 recycled; write=0.000 s, sync=0.007 s, total=0.037 s; sync files=1, longest=0.007 s, average=0.007 s; distance=65535 kB, estimate=65537 kB
2020-08-07 10:12:03 UTC::@:[13368]:LOG: database system is shut down
2020-08-07 10:12:06 UTC::@:[28963]:LOG: database system was shut down at 2020-08-07 10:12:03 UTC
2020-08-07 10:12:06 UTC::@:[28961]:LOG: database system is ready to accept connections
2020-08-07 10:17:06 UTC::@:[28964]:LOG: checkpoint starting: time
2020-08-07 10:17:07 UTC::@:[28964]:LOG: checkpoint complete: wrote 3 buffers (0.0%); 0 WAL file(s) added, 0 removed, 1 recycled; write=0.881 s, sync=0.006 s, total=0.921 s; sync files=3, longest=0.006 s, average=0.002 s; distance=16 kB, estimate=16 kB
2020-08-07 10:22:06 UTC::@:[28964]:LOG: checkpoint starting: time
2020-08-07 10:22:06 UTC::@:[28964]:LOG: checkpoint complete: wrote 1 buffers (0.0%); 0 WAL file(s) added, 0 removed, 1 recycled; write=0.678 s, sync=0.006 s, total=0.712 s; sync files=1, longest=0.006 s, average=0.006 s; distance=65521 kB, estimate=65521 kB
2020-08-07 10:27:06 UTC::@:[28964]:LOG: checkpoint starting: time
2020-08-07 10:27:07 UTC::@:[28964]:LOG: checkpoint complete: wrote 1 buffers (0.0%); 0 WAL file(s) added, 0 removed, 1 recycled; write=0.693 s, sync=0.006 s, total=0.727 s; sync files=1, longest=0.006 s, average=0.006 s; distance=65536 kB, estimate=65536 kB
2020-08-07 10:32:06 UTC::@:[28964]:LOG: checkpoint starting: time
2020-08-07 10:32:06 UTC::@:[28964]:LOG: checkpoint complete: wrote 1 buffers (0.0%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.101 s, sync=0.005 s, total=0.131 s; sync files=1, longest=0.005 s, average=0.005 s; distance=65536 kB, estimate=65536 kB
2020-08-07 10:37:06 UTC::@:[28964]:LOG: checkpoint starting: time
2020-08-07 10:37:06 UTC::@:[28964]:LOG: checkpoint complete: wrote 1 buffers (0.0%); 0 WAL file(s) added, 0 removed, 1 recycled; write=0.101 s, sync=0.005 s, total=0.131 s; sync files=1, longest=0.005 s, average=0.005 s; distance=65535 kB, estimate=65536 kB
2020-08-07 10:42:06 UTC::@:[28964]:LOG: checkpoint starting: time
2020-08-07 10:42:06 UTC::@:[28964]:LOG: checkpoint complete: wrote 1 buffers (0.0%); 0 WAL file(s) added, 0 removed, 1 recycled; write=0.101 s, sync=0.005 s, total=0.132 s; sync files=1, longest=0.005 s, average=0.005 s; distance=65536 kB, estimate=65536 kB
2020-08-07 10:47:06 UTC::@:[28964]:LOG: checkpoint starting: time
2020-08-07 10:47:06 UTC::@:[28964]:LOG: checkpoint complete: wrote 1 buffers (0.0%); 0 WAL file(s) added, 0 removed, 1 recycled; write=0.101 s, sync=0.005 s, total=0.135 s; sync files=1, longest=0.005 s, average=0.005 s; distance=65536 kB, estimate=65536 kB
2020-08-07 10:52:06 UTC::@:[28964]:LOG: checkpoint starting: time
2020-08-07 10:52:07 UTC::@:[28964]:LOG: checkpoint complete: wrote 1 buffers (0.0%); 0 WAL file(s) added, 0 removed, 1 recycled; write=0.101 s, sync=0.006 s, total=0.133 s; sync files=1, longest=0.006 s, average=0.006 s; distance=65535 kB, estimate=65536 kB
2020-08-07 10:57:06 UTC::@:[28964]:LOG: checkpoint starting: time
2020-08-07 10:57:06 UTC::@:[28964]:LOG: checkpoint complete: wrote 1 buffers (0.0%); 0 WAL file(s) added, 0 removed, 1 recycled; write=0.101 s, sync=0.005 s, total=0.137 s; sync files=1, longest=0.005 s, average=0.005 s; distance=65536 kB, estimate=65536 kB
----------------------- END OF LOG --

我尝试测试服务器上的端口,重新启动我的 RDS 实例但没有任何效果,我也卸载并重新安装了 psycopg2 和 sqlalchemy。

我可以从命令行连接到数据库。我使用 nmap 测试了端口,得到以下结果:

PORT     STATE    SERVICE
5432/tcp filtered postgresql

我仍然非常困惑,应用程序在过去 10 个小时里一直无法使用!我不知道这是怎么回事,我很想得到一些帮助,我开始在工作中感到压力。谢谢

更新:我刚刚注意到,在 中的 aws 控制台中database/mydb/configurations/parameters group,它显示default.postgres11 (in-sync)。这可能是升级后突然出现问题的原因吗?我不记得之前说过同步,我想知道它会持续多久

答案1

psql:无法连接到服务器:连接超时 服务器是否正在运行主机“IP”并在端口 5432 上接受 TCP/IP 连接?

您确实连接到IP地址而是它的DNS 名称? 这可能是问题所在 - 当更改 RDS 大小时,IP 也发生了变化。事实上,它有时会意外地发生变化。

切勿直接使用 IP(例如172.31.xx),始终使用类似myrds.xxx.us-west-2.rds.amazonaws.com

希望有帮助:)

相关内容