Introduction
This article is showing how to perform the one exist as the description in the title of the article. The title of this article which is ‘How to Force Drop PostgreSQL Database in PostgreSQL Database Server’. So, this article is focusing on how to perform the removal process of a PostgreSQL database. In other words, it is how to drop or to erase completely a PostgreSQL database. In this case, the removal process is in a PostgreSQL database server in a device running using Microsoft Windows. What is the main purpose having to perform a force removal of a PostgreSQL database server ?. The reason is because of the existing connectivity to the PostgreSQL database. Normally, removing or erasing PostgreSQL database normal while there is an active connectivity will ends in a failure. Failure in terms of failing to remove or to erase PostgreSQL database as long as there is an active connection. So, in order to remove or to drop database ignoring the active connectivity exist to the database, just use the force option to do it.
How to Force Drop PostgreSQL Database
So, this part will show the sequence for performing the task. For the sequence, the following is the actual steps :
-
First of all, connect to the PostgreSQL database server. In this case, it is using command line :
(env) C:\python>python Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>
-
After successfully connecting to the PostgreSQL database server, just check the available database first
postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -------------------+----------+----------+----------------------------+----------------------------+----------------------- mydb | postgres | UTF8 | English_United States.1252 | English_United States.1252 | postgis_32_sample | postgres | UTF8 | English_United States.1252 | English_United States.1252 | postgres | postgres | UTF8 | English_United States.1252 | English_United States.1252 | template0 | postgres | UTF8 | English_United States.1252 | English_United States.1252 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | English_United States.1252 | English_United States.1252 | =c/postgres + | | | | | postgres=CTc/postgres (5 rows) postgres=#
-
After successfully listing the database available in the PostgreSQL database server, just start another query or command to drop the database as follows :
postgres=# drop database mydb; ERROR: database "mydb" is being accessed by other users DETAIL: There are 3 other sessions using the database. postgres=#
-
Since the command which is actually performing a query above ends in a failure because of the active connection to the database, just try another alternative command or query to force drop database :
postgres=# drop database mydb with(force); DROP DATABASE postgres=#
-
Finally, check the available database once more. It should not list the already removed one as in the execution above.
postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -------------------+----------+----------+----------------------------+----------------------------+----------------------- postgis_32_sample | postgres | UTF8 | English_United States.1252 | English_United States.1252 | postgres | postgres | UTF8 | English_United States.1252 | English_United States.1252 | template0 | postgres | UTF8 | English_United States.1252 | English_United States.1252 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | English_United States.1252 | English_United States.1252 | =c/postgres + | | | | | postgres=CTc/postgres (4 rows) postgres=#