When using the view (pg_stat_activity), follow the procedure below to close a connection.
Use psql command to connect to the postgres database.
> psql postgres
psql (<x>) (*1)
Type "help" for help.
*1: <x> indicates the PostgreSQL version on which this product is based.
Close connections from clients that have been in the waiting state for an extended period.
Use pg_terminate_backend() to close connections that have been trying to connect for an extended period.
However, when considering continued compatibility of applications, do not reference or use system catalogs and functions directly in SQL statements. Refer to " Notes on Application Compatibility" in the Application Development Guide for details.
The following example closes connections where the client has been in the waiting state for at least 60 minutes.
select pid,usename,application_name,client_addr,pg_terminate_backend(pid) from pg_stat_activity where backend_type = 'client backend' and state='idle in transaction' and current_timestamp > cast(query_start + interval '60 minutes' as timestamp); -[ RECORD 1 ]--------+--------------- pid | 4684 username | fsepuser application_name | apl1 client_addr | 192.11.11.1 pg_terminate_backend | t
See
Refer to "System Administration Functions" under "The SQL Language" in the PostgreSQL Documentation for information on pg_terminate_backend.
Refer to "Notes on Application Compatibility" in the Application Development Guide for information on how to maintain application compatibility.