使用 psycopg2 关闭现有的 Postgres 连接

Close an existing Postgres connection using psycopg2

我在玩 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 start/close 这些连接,我将不胜感激?

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

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