First find all current procesid using your database:
SELECT usename, procpid FROM pg_stat_activity WHERE datname = current_database();
Secondly, kill processes that you don't need:
SELECT pg_terminate_backend(your_procpid);
This works with version 8.4, otherwise pg_terminate_backend () is unknown and you need to kill the process at OS level.
To quickly remove all connections connected to this database, this shortcut works beautifully. Should work as superuser:
SELECT pg_terminate_backend(procpid) FROM pg_stat_activity WHERE datname='YourDB';
In later versions of Postgres (at least 9.2+, most likely earlier), the column names changed and the query:
SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname='YourDB';
source share