Introduction
An error appear upon executing a certain query in the PostgreSQL database server is exist as part of the discussion in this article. Actually, the query is for dropping a database exist in a PostgreSQL database server. In this case, the process for dropping or removing the PostgreSQL database is in a local device running using Microsoft Windows. Before going further to the solution, the following is the actual execution of the query which is triggering the error message :
C:\>psql -Upostgres Password for user postgres: psql (14.2) WARNING: Console code page (437) differs from Windows code page (1252) 8-bit characters might not work correctly. See psql reference page "Notes for Windows users" for details. Type "help" for help. postgres=# drop database mydb; ERROR: database "mydb" is being accessed by other users DETAIL: There are 3 other sessions using the database.
So, the process for executing the query ends in failure as in the above output command. The reason is actually exist as part of the error message. So, the process for dropping or removing the PostgreSQL database end in failure because it is currently accessed by several users. In the above output, it is being accessed by 3 users. That is why because it currently utilized by several users, the drop or the removal process ends in failure.
How to Solve Error Message ERROR: database “database_name” is being accessed by other users
So, this part is actually focusing onto the solution. In other words, just find a way to drop or to remove the database although it is currently still in used. Morevoer, there are several user currently using it. In other words, there are several connections from several user to the database. So, the process for removing or dropping PostgreSQL database server is not possible. Actually, there is some sort of mechanism for doing that. Just execute the drop database query with an additional parameter. This additional parameter is for forcing to drop or to remove PostgreSQL database server despite of several user connectivity. Just perform the following query as follows :
C:\>psql -Upostgres Password for user postgres: psql (14.2) WARNING: Console code page (437) differs from Windows code page (1252) 8-bit characters might not work correctly. See psql reference page "Notes for Windows users" for details. Type "help" for help. postgres=# drop database mydb with(force); DROP DATABASES
As it exist in the above query command execution, there is an additional parameter to be able to do it. That additional parameter is ‘with(force)’. And finally, it ends in a success.