SELECT * FROM pg_stat_activity WHERE state = 'active';
So you can identify the PID of the hanging query you want to terminate, run this:
SELECT pg_cancel_backend(PID);
S. Durand, and N. Marchand. Proceedings of the European Control Conference 2009, page 1979-1984. Budapest, Hongrie, (August 2009)Département Automatique Département Automatique.