How can I kill all my postgresql connections?
I\'m trying a rake db:drop
but I get:
ERROR: database \"database_name\" is being accessed
Quit postgres and restart it. Simple, but works every time for me, where other cli commands sometimes don't.
With all infos about the running process:
SELECT *, pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE pid <> pg_backend_pid()
AND datname = 'my_database_name';
I had this issue and the problem was that Navicat was connected to my local Postgres db. Once I disconnected Navicat the problem disappeared.
EDIT:
Also, as an absolute last resort you can back up your data then run this command:
sudo kill -15 `ps -u postgres -o pid`
... which will kill everything that the postgres user is accessing. Avoid doing this on a production machine but you shouldn't have a problem with a development environment. It is vital that you ensure every postgres
process has really terminated before attempting to restart PostgreSQL after this.
EDIT 2:
Due to this unix.SE post I've changed from kill -9
to kill -15
.
OSX, Postgres 9.2 (installed with homebrew)
$ launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
$ pg_ctl restart -D /usr/local/var/postgres
$ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
If your datadir is elsewhere you can find out where it is by examining the output of ps aux | grep postgres
SELECT
pg_terminate_backend(pid)
FROM
pg_stat_activity
WHERE
pid <> pg_backend_pid()
-- no need to kill connections to other databases
AND datname = current_database();
-- use current_database by opening right query tool
I'VE SOLVED THIS WAY:
In my Windows8 64 bit, just restart
ing the service: postgresql-x64-9.5