First, check all the processes that are running:
SELECT * FROM pg_stat_activity;
So you can identify the PID of the hanging query you want to terminate, run this:
Option #1 (graceful):
SELECT pg_cancel_backend(PID);
Option #2 (forceful):
SELECT pg_terminate_backend(PID);
SELECT
pg_terminate_backend(pid)
FROM
pg_stat_activity
WHERE
-- don't kill my own connection!
pid <> pg_backend_pid()
AND state = 'idle'