Read more

Postgres: How to force database sessions to terminate

Emanuel
August 29, 2017Software engineer at makandra GmbH

If another session is accessing your database you are trying to reset or drop you might have seen the following error:

PG::ObjectInUse: ERROR:  database "foo_development" is being accessed by other users
DETAIL:  There is 1 other session using the database.
Illustration book lover

Growing Rails Applications in Practice

Check out our e-book. Learn to structure large Ruby on Rails codebases with the tools you already know and love.

  • Introduce design conventions for controllers and user-facing models
  • Create a system for growth
  • Build applications to last
Read more Show archive.org snapshot

This could be the rails server, rubymine and many more. Beside terminating the session connection manually you can also find out the pid and kill the process.

1. rails db
2. SELECT * FROM pg_stat_activity;

datid            | 98359
datname          | foo_development
pid              | 21633
usesysid         | 16384
usename          | username
application_name | DataGrip 2017.2.1
client_addr      | 127.0.0.1
client_hostname  | [NULL]
client_port      | 53682
backend_start    | 2017-08-29 09:19:04.080051+02
xact_start       | [NULL]
query_start      | 2017-08-29 09:19:04.213843+02
state_change     | 2017-08-29 09:19:04.213868+02
waiting          | f
state            | idle
backend_xid      | [NULL]
backend_xmin     | [NULL]
query            | select current_database() as a, current_schemas(false) as b

3. \q
4. sudo kill 21633

Note: The background of this card is a development environment. In production it might be a bad idea to simply kill database sessions.

Posted by Emanuel to makandra dev (2017-08-29 09:12)