Last Updated: February 25, 2016
·
1.228K
· nicooga

Postgres: terminanting db connections.

Next time you get stuck with a message like:

ActiveRecord::StatementInvalid: PG::ObjectInUse: ERROR:  database "bummer_test" is being accessed by other users
DETAIL:  There is 1 other session using the database.

try killing all the connections on the db. Login into the DB console, either by just using psql -d $(YOUR_DB_NAME), or su postgres -c psql or bundle exec rails dbconsole.

.. then:

SELECT pg_terminate_backend(psa.pid)
  FROM pg_stat_activity AS psa
  WHERE psa.datname = '<your_db_name>' 
    AND psa.pid <> pg_backend_pid(); -- don't terminate your own connection