PostgreSQL current connection number

Disconnect to a Postgres database

Mischa Sameli, Managing Director & Head of Development

How do you delete a database that is currently in use? Sure, make all connections and execute the drop statement - not that easy with PostgreSQL and pgAdmin.

While you can easily disconnect the active connections in the Enterprise Manager for MSSQL, for example, Postgres' board tool, pgAdmin, does not offer such a possibility. SQL can help. First of all, we choose a database - not the one we want to disconnect from and which should ultimately be deleted. Then we use PostgreSQL from Version 9.2 the following SQL statement:

SELECT pg_terminate_backend (pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'mydatabase'; 1SELECT pg_terminate_backend (pg_stat_activity.pid)
2FROM pg_stat_activity
3WHERE pg_stat_activity.datname = 'mydatabase';

For Postgres 9.1 and older the view pg_stat_activity provides slightly different results. So the command is a little different:

SELECT pg_terminate_backend (pg_stat_activity.procpid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'mydatabase'; 1SELECT pg_terminate_backend (pg_stat_activity.procpid)
2FROM pg_stat_activity
3WHERE pg_stat_activity.datname = 'mydatabase';

Nice that you liked our article. Use the following social networking services to file and distribute the post. You can of course bookmark this article directly.

Functions

Amounts from this author

Links to content

Links on the subject