Other

How do you drop a PostgreSQL database if there are active connections to it?

How do you drop a PostgreSQL database if there are active connections to it?

In PostgreSQL 9.2 and above, to disconnect everything except your session from the database you are connected to: SELECT pg_terminate_backend(pg_stat_activity. pid) FROM pg_stat_activity WHERE datname = current_database() AND pid <> pg_backend_pid(); In older versions it’s the same, just change pid to procpid .

How do I drop a database in PostgreSQL?

Introduction to PostgreSQL DROP DATABASE statement To delete a database: Specify the name of the database that you want to delete after the DROP DATABASE clause. Use IF EXISTS to prevent an error from removing a non-existent database. PostgreSQL will issue a notice instead.

How do I see active connections in PostgreSQL?

In the Browser pane, select our database (1) and then click on the Dashboard tab (2). In the bottom of page there is Server Activity panel which contain all connected sessions (3).

How to drop all connections to a specific PostgreSQL database?

I want to drop all connections (sessions) that are currently opened to a specific PostgreSQL database but without restarting the server or disconnecting connections to other databases. How can I do that? Here is my answer to very similar question on StackOverflow.

How to list active connections in PostgreSQL query?

Since pg_stat_activity contains connection statistics of all databases having any state, either idle or active, database name and connection state should be included in the query to get the desired output. Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question. Provide details and share your research! But avoid …

How to delete active databases in PostgreSQL?

To delete the database that has active connections, you can follow these steps: First, find the activities associated with the database by querying the pg_stat_activity view: Second, terminate the active connections by issuing the following query:

How to drop the testdb1 database in PostgreSQL?

To drop the testdb1 database, you need to terminate the active connection and drop the database. First, query the pg_stat_activity view to find what activities are taking place against the testdb1 database: The testdb1 database has one connection from localhost therefore it is safe to terminate this connection and remove the database.