问题
I was playing around with Postgresql
and psycopg2
. I think I started many connections using the terminal but never closed it. Using pyscopg2
I understood how to start a connection and close it too. Now I was trying to get the existing connection (that i launched using the terminal before) using pyscopg2
but it seems there is an issue with port number.
When I run SELECT * FROM pg_stat_activity ;
, these are my results
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
The connection at 127.0.0.1
, at port 54156
is what I want to close. So I thought I could get this exisiting connection in psycopg2
using the following lines :
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()
But it gives me the error :
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?
Probably the connection is not active on this IP and port number, so i did a netstat | grep postgres
to confirm if the connections are still active and these are the results :
MBP-di-Siddhant:agensgraph siddhanttandon$ netstat | grep postgres
tcp4 0 0 localhost.postgresql localhost.54156 ESTABLISHED
tcp4 0 0 localhost.54156 localhost.postgresql ESTABLISHED
Ideally I would like to control the setting up of connection to existing dbs and closing the connections using python instead of using the command line interface.
I would really appreciate if someone could tell me how can I start/close these connections using psycopg2
?
回答1:
You can't just use the client port number to somehow invade and take over the connection.
If you want force the connection to close, you use the pid. From the linux command line: kill 77115
or from the SQL command line (or from psycopg2 over a different connection): select pg_terminate_backend(77115)
.
来源:https://stackoverflow.com/questions/59734370/close-an-existing-postgres-connection-using-psycopg2