【问题标题】:Close an existing Postgres connection using psycopg2使用 psycopg2 关闭现有的 Postgres 连接
【发布时间】:2020-01-14 23:40:32
【问题描述】:

我在玩 Postgresqlpsycopg2。我想我使用终端开始了许多连接,但从未关闭它。使用pyscopg2,我了解了如何启动和关闭连接。现在我试图使用pyscopg2 获取现有连接(我之前使用终端启动),但端口号似乎存在问题。

当我运行SELECT * FROM pg_stat_activity ; 时,这些是我的结果

 datid |    datname     |  pid  | usesysid |    usename     | application_name | client_addr | client_hostname | client_port |         backend_start         |          xact_start           |          query_start          |         state_change          | wait_event_type |     wait_event      |        state        | backend_xid | backend_xmin |                             query                              |    backend_type     
-------+----------------+-------+----------+----------------+------------------+-------------+-----------------+-------------+-------------------------------+-------------------------------+-------------------------------+-------------------------------+-----------------+---------------------+---------------------+-------------+--------------+----------------------------------------------------------------+---------------------
       |                | 75600 |          |                |                  |             |                 |             | 2020-01-13 15:28:42.66597+01  |                               |                               |                               | Activity        | AutoVacuumMain      |                     |             |              |                                                                | autovacuum launcher
       |                | 75602 |       10 | siddhanttandon |                  |             |                 |             | 2020-01-13 15:28:42.666037+01 |                               |                               |                               | Activity        | LogicalLauncherMain |                     |             |              |                                                                | background worker
 16385 | siddhanttandon | 77470 |       10 | siddhanttandon | agens            |             |                 |          -1 | 2020-01-13 16:04:04.907286+01 |                               |                               | 2020-01-13 16:04:04.910102+01 | Client          | ClientRead          | idle                |             |              |                                                                | client backend
 16385 | siddhanttandon | 77115 |       10 | siddhanttandon |                  | 127.0.0.1   |                 |       54156 | 2020-01-13 15:45:08.361864+01 | 2020-01-13 15:45:08.365267+01 | 2020-01-13 15:45:08.366289+01 | 2020-01-13 15:45:08.369882+01 | Client          | ClientRead          | idle in transaction |             |              | MATCH (a)-[r]->(b) RETURN id(a) AS startNode, id(b) AS endNode | client backend
 16385 | siddhanttandon | 82701 |       10 | siddhanttandon | agens            |             |                 |          -1 | 2020-01-14 12:08:16.601504+01 | 2020-01-14 13:16:55.356656+01 | 2020-01-14 13:16:55.356656+01 | 2020-01-14 13:16:55.35666+01  |                 |                     | active              |             |          565 | SELECT * FROM pg_stat_activity ;                               | client backend
       |                | 75598 |          |                |                  |             |                 |             | 2020-01-13 15:28:42.662682+01 |                               |                               |                               | Activity        | BgWriterHibernate   |                     |             |              |                                                                | background writer
       |                | 75597 |          |                |                  |             |                 |             | 2020-01-13 15:28:42.662907+01 |                               |                               |                               | Activity        | CheckpointerMain    |                     |             |              |                                                                | checkpointer
       |                | 75599 |          |                |                  |             |                 |             | 2020-01-13 15:28:42.6631+01   |                               |                               |                               | Activity        | WalWriterMain       |                     |             |              |                                                                | walwriter 

127.0.0.1 端口 54156 的连接是我想要关闭的。所以我想我可以使用以下几行在psycopg2 中获得这个现有的连接:

import psycopg2.pool
dbpool = psycopg2.pool.ThreadedConnectionPool(minconn=5,maxconn=25,host='127.0.0.1',
                                          port='54156',
                                          dbname='test_db',
                                          user='siddhanttandon'
                                          )
dbpool.closeall()

但它给了我错误:

could not connect to server: Connection refused
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 54156?
could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 54156?

可能此 IP 和端口号上的连接不活跃,所以我做了一个netstat | grep postgres 来确认连接是否仍然活跃,结果如下:

MBP-di-Siddhant:agensgraph siddhanttandon$ netstat | grep postgres
tcp4       0      0  localhost.postgresql   localhost.54156        ESTABLISHED
tcp4       0      0  localhost.54156        localhost.postgresql   ESTABLISHED

理想情况下,我想控制与现有数据库的连接设置,并使用 python 而不是使用命令行界面关闭连接。

如果有人能告诉我如何使用 psycopg2 启动/关闭这些连接,我将不胜感激?

【问题讨论】:

  • 这就是你在运行.closeall() 后应该期待的结果。
  • 在完成.closeall() 之后,我再次运行netstat | grep postgres,我仍然在结果中获得127.0.0.1 的连接。我对结果的解释有误吗?

标签: python postgresql psycopg2


【解决方案1】:

您不能只使用客户端端口号以某种方式入侵并接管连接。

如果要强制关闭连接,请使用 pid。从 linux 命令行:kill 77115 或从 SQL 命令行(或通过不同的连接从 psycopg2):select pg_terminate_backend(77115)

【讨论】:

  • 感谢您的回复。你能解释一下第一行吗?我认为我在 pids 和端口之间混淆了一些东西。多个postgress进程在后台运行是否正常?
猜你喜欢
  • 2020-03-27
  • 2018-04-11
  • 1970-01-01
  • 1970-01-01
  • 2019-10-10
  • 2013-12-11
  • 2012-03-14
  • 2020-10-09
相关资源
最近更新 更多