Posted almost 3 years ago. Visible to the public.

Postgres: How to force database sessions to terminate

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.

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 | 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.

makandra has been working exclusively with Ruby on Rails since 2007. Our laser focus on a single technology has made us a leader in this space.

Owner of this card:

Emanuel De
Last edit:
almost 3 years ago
by Emanuel De
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Emanuel De to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more