All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

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

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.

12 Apr 10:07

What is Routing?

meme code
meme code@memecode
14 Apr 09:12

What are Quality values (quality values / priority) in HTTP?

meme code
meme code@memecode
14 Apr 09:14

Accept - HTTP header, what is it for and how does it work?

meme code
meme code@memecode
15 Apr 17:50

What is entropy?

meme code
meme code@memecode
15 Apr 18:11

What are HTTP Client Hints?

meme code
meme code@memecode
18 Apr 05:27

We fix ActiveRecord::ProtectedEnvironmentError in the local environment.

meme code
meme code@memecode
27 Apr 09:29

What are CC and BCC in emails? What are they for and how to use them?

meme code
meme code@memecode
What is a prompt and prompt engineering?
03 May 12:08

What is a prompt and prompt engineering?

meme code
meme code@memecode
ZOMBIE in Ruby. What is it?
03 May 12:41

ZOMBIE in Ruby. What is it?

meme code
meme code@memecode
03 May 13:13

What is a Garbage Collector in Ruby? How does it work and what is GC needed for?

meme code
meme code@memecode
A bit about Ruby implementation types (CRuby (MRI), JRuby, Rubinius, TruffleRuby, mruby)
05 May 12:36

A bit about Ruby implementation types (CRuby (MRI), JRuby, Rubinius, TruffleRuby, mruby)

meme code
meme code@memecode
07 May 07:24

What is native machine code?

meme code
meme code@memecode