Find and kill the hang query or session in PostgreSQL
Check all the running processes in PostgreSQL
SELECT pid,user,query FROM pg_stat_activity WHERE state = 'active';
Terminate the execution of the query which you want to stop:
Get the process id from the first command and cancel the query with the use of the following command :
SELECT pg_cancel_backend(PID);
Terminate the complete session :
To kill or terminate the session of the user running or executing a hanging query:
SELECT pg_terminate_backend(PID);