One, to see which users are linked to the database
Select from Pg_stat_activity;
The pg_stat_activity here is actually a view, and its definition can be found in the View section of the Postgres database.
Second, kill the process now we find all the processes connected to the database, then how to kill those Idel process to release the connection? If the PG version is 8.4 and above, it is easy to kill all Idel processes with the following statement:
SELECT from WHERE current_query='<IDLE>'
Pg_terminate_backend is the internal method of PG, and there is another called Pg_cancel_backend, which has existed in the previous version of 8.4. The difference between the two methods is:
- Pg_cancel_backend just cancels the query operation of one of the current processes, but cannot release the database connection
- Pg_terminate_backend can kill this process in the background of PG, thus freeing up valuable connection resources
POSTGRESQL View user usage and kill the corresponding process