簡體   English   中英

使用 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 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啟動/關閉這些連接,我將不勝感激?

您不能僅使用客戶端端口號以某種方式侵入並接管連接。

如果要強制關閉連接,請使用 pid。 從 linux 命令行: kill 77115或從 SQL 命令行(或從 psycopg2 通過不同的連接): select pg_terminate_backend(77115)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM