← homeProgramming (Програмування)

Analyzing the PG::ObjectInUse error (Ruby on Rails)

What does the error PG::ObjectInUse (Ruby on Rails) mean? How to fix database is being accessed by other users

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.

🔥 More posts

All posts
Programming (Програмування)Apr 12, '24 10:07

What is Routing?

What is Routing? What is routing needed for in IT?

Programming (Програмування)Apr 15, '24 17:50

What is entropy?

What is entropy? What does the level of entropy in IT indicate?

Programming (Програмування)Apr 15, '24 18:11

What are HTTP Client Hints?

What are HTTP Client hints? What are HTTP Client hints used for?

ZOMBIE in Ruby. What is it?
Programming (Програмування)May 3, '24 12:41

ZOMBIE in Ruby. What is it?

ZOMBIE in Ruby. What is it? What is this term used for in the Ruby repository?

Programming (Програмування)May 7, '24 07:24

What is native machine code?

What is native machine code? How does it work and what is it used for?