Linux 2的Windows子系统:Ubuntu,连接到外部PostgreSQL数据库

2024-04-19 03:26:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Ubuntu18.04运行新的WindowsSubsystem for Linux2(WSL-2)。它真的很快,而且运行得很好,只是我似乎无法使用Python连接到外部PostgreSQL数据库。它只是挂着,从不回应。这里有一个最小的复制:

$ python3.6
Python 3.6.8 (default, Jan 14 2019, 11:02:34)
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
>>> psycopg2.connect(host="my-pg-server.mydomain.com", port=5432, user="my_user", dbname="my_db", password="")
[...crickets... doesn't time out, just hangs forever (at least an hour)...]

主机与telnet连接的问题不同:

^{2}$

另一个奇怪的地方是我可以连接到外部sqlserver数据库。我确信这两个服务器的凭据都是正确的,它们直接来自于我在其他系统上使用的Django设置文件。有什么想法吗?我有没有什么事情要特别针对WSL-2来处理psycopg2?在


Tags: 数据库defaultforpostgresqlmyrevisionpsycopg2jan
1条回答
网友
1楼 · 发布于 2024-04-19 03:26:00

结果我把手指指向了错误的方向。在

我和一位同事登录到PostgreSQL server box,并发出以下命令:

ps -ef sort=start_time | fgrep [db host name] | more

结果发现,与服务器的现有连接还不错,但有些东西被阻塞了。我有一堆空闲进程,然后有一大堆进程在说“启动等待”——超过100个。以下是命令的输出:

[...about 100 idle processes, truncated...]
postgres 26815 48821  0 Aug16 ? 00:00:00 postgres: my-pg-server: web_user web 192.168.9.187(55972) idle
postgres 27525 48821  0 Aug16 ? 00:00:00 postgres: my-pg-server: web_user web 192.168.9.187(55976) idle
postgres 14781 48821  0 00:00 ? 00:00:00 postgres: my-pg-server: postgres jsmith_d [local] VACUUM waiting
postgres 22738 48821  0 00:01 ? 00:00:00 postgres: my-pg-server: other_user other_db 192.168.9.187(57692) startup waiting
postgres  7683 48821  0 00:15 ? 00:00:00 postgres: my-pg-server: yetanother_user yetanother_db 192.168.9.187(57694) startup waiting
postgres 15951 48821  0 00:30 ? 00:00:00 postgres: my-pg-server: yetanother_user yetanother_db 192.168.9.187(57696) startup waiting
[...and about another 100 startup waiting processes, truncated...]

啊哈!找到了罪犯:

postgres 14781 48821 0 00:00 ? 00:00:00 postgres: my-pg-server: postgres jsmith_d [local] VACUUM waiting

VACUUM进程中似乎有什么东西卡住了,这导致新的连接挂起,而没有失败。是时候更深入地挖掘和清理它了,但是这种行为是有答案的。在

相关问题 更多 >