Whenever I try to drop database I get:
ERROR: database \"pilot\" is being accessed by other users
DETAIL: There is 1 other session using the database.
Stop your running application.(in Eclipse) After you try again.
GUI solution using pgAdmin 4
First enable show activity on dashboard if you haven't:
File > Preferences > Dashboards > Display > Show Activity > true
Now disable all the processes using the db:
You should now be able to delete the db.
You can prevent future connections:
REVOKE CONNECT ON DATABASE thedb FROM public;
(and possibly other users/roles; see \l+
in psql
)
You can then terminate all connections to this db except your own:
SELECT pid, pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE datname = current_database() AND pid <> pg_backend_pid();
On older versions pid
was called procpid
so you'll have to deal with that.
Since you've revoked CONNECT
rights, whatever was trying to auto-connect should no longer be able to do so.
You'll now be able to drop the DB.
This won't work if you're using superuser connections for normal operations, but if you're doing that you need to fix that problem first.
After you're done dropping the database, if you create the database again, you can execute below command to restore the access
GRANT CONNECT ON DATABASE thedb TO public;
Solution:
1. Shut down Pg server
2. It will disconnect all active connection
3. Restart Pg Server
4. Try your command
Whenever I try to drop database I get:
ERROR: database "pilot" is being accessed by other users
DETAIL: There is 1 other session using the database.
First You need to revoke
REVOKE CONNECT ON DATABASE TARGET_DB FROM public;
Then use:
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'TARGET_DB';
It will surely work.
Simply check what is the connection, where it's coming from. You can see all this in:
SELECT * FROM pg_stat_activity WHERE datname = 'TARGET_DB';
Perhaps it is your connection?