SoFunction
Updated on 2025-04-07

postgres connection count viewing and setting operations

There is a table in PG that records how many connections are currently there

Table name: pg_stat_activity

Query the current number of connections

select count(1) from pg_stat_activity;

Query the maximum number of connections

show max_connections;

The maximum number of connections can also be configured in the pg configuration file:

Set in:

max_connections = 500

Additional: Too many connections in Postgresql

as follows:

//View expired connectionselect * from pg_stat_activity where state = 'idle'
 
//Delete the connection and pass the pid in bracketsselect pg_terminate_backend(25800);
 
//View the maximum number of connectionsshow max_connections;
 
//Modify the maximum number of connections, superuser permission is requiredalter system set max_connections= 1000;

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.