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 online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
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)