This content has been automatically translated from Ukrainian.
Sometimes during development, you may encounter the error PG::ObjectInUse: ERROR.
For example:
rake db:drop;
It will show us the following error:
PG::ObjectInUse: ERROR: database "myproject_development" is being accessed by other users DETAIL: There is 1 other session using the database Couldn't drop database 'myproject_development' rake aborted! ActiveRecord::StatementInvalid: PG::ObjectInUse: ERROR: database "myproject_development" is being accessed by other users DETAIL: There is 1 other session using the database. Caused by:PG::ObjectInUse: ERROR: database "myproject_development" is being accessed by other users DETAIL: There is 1 other session using the database. Tasks: TOP => db:drop:_unsafe (See full trace by running task with --trace)
Everything here is quite simple. The message is very informative and literally says - database "myproject_development" is being accessed by other users. This means that we cannot drop the database at the moment because there is an active connection to it. Locally, this is usually either a running rails server and/or rails c (console). They need to be closed to disconnect the process from the database. After that, we restart rake db:drop; and everything should work.
Sometimes it may happen that locally there seem to be no open processes/connections in the terminal, but we still have the error "database is being accessed by other users". Usually, this means that we have a process somewhere that is still working with the database. Sometimes it may be a background process/daemon. But to find and kill this process, more context is needed. You can scan ports and processes and kill those that are using the database. But I will probably write about this in another note.
This post doesn't have any additions from the author yet.