Dan*_*anS 342 database postgresql
如何杀死所有postgresql连接?
我正在尝试rake db:drop但是我得到了:
ERROR: database "database_name" is being accessed by other users
DETAIL: There are 1 other session(s) using the database.
Run Code Online (Sandbox Code Playgroud)
我已经尝试关闭我从a看到的进程,ps -ef | grep postgres但这也不起作用:
kill: kill 2358 failed: operation not permitted
Run Code Online (Sandbox Code Playgroud)
Fra*_*ens 613
您可以使用pg_terminate_backend()来终止连接.您必须是超级用户才能使用此功能.这适用于所有操作系统.
SELECT
pg_terminate_backend(pid)
FROM
pg_stat_activity
WHERE
-- don't kill my own connection!
pid <> pg_backend_pid()
-- don't kill the connections to other databases
AND datname = 'database_name'
;
Run Code Online (Sandbox Code Playgroud)
执行这个查询之前,你必须REVOKE的CONNECT权限,以避免新的连接:
REVOKE CONNECT ON DATABASE dbname FROM PUBLIC, username;
Run Code Online (Sandbox Code Playgroud)
如果您使用的是Postgres 8.4-9.1,请使用procpid而不是pid
SELECT
pg_terminate_backend(procpid)
FROM
pg_stat_activity
WHERE
-- don't kill my own connection!
procpid <> pg_backend_pid()
-- don't kill the connections to other databases
AND datname = 'database_name'
;
Run Code Online (Sandbox Code Playgroud)
Har*_*ina 194
也许只是重启postgres=>sudo service postgresql restart
Dor*_*ian 22
有关运行过程的所有信息:
SELECT *, pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE pid <> pg_backend_pid()
AND datname = 'my_database_name';
Run Code Online (Sandbox Code Playgroud)
Juu*_*nen 14
MacOS,如果postgresql是用brew安装的:
brew services restart postgresql
Run Code Online (Sandbox Code Playgroud)
小智 13
如果您需要断开特定用户的会话,这对我有帮助:
检查当前所有连接:
select * from pg_stat_activity;
Run Code Online (Sandbox Code Playgroud)
向您的用户授予角色(不重要):
set role "db_admin";
Run Code Online (Sandbox Code Playgroud)
杀死会话:
select pg_terminate_backend(pid)
from pg_stat_activity
where usename = '*** USER NAME TO DISCONNECT ***';
Run Code Online (Sandbox Code Playgroud)
art*_*ave 12
OSX,Postgres 9.2(随自制软件一起安装)
$ 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
Run Code Online (Sandbox Code Playgroud)
如果你的datadir在别处你可以通过检查输出来找出它的位置 ps aux | grep postgres
这似乎适用于PostgreSQL 9.1:
#{Rails.root}/lib/tasks/databases.rake
# monkey patch ActiveRecord to avoid There are n other session(s) using the database.
def drop_database(config)
case config['adapter']
when /mysql/
ActiveRecord::Base.establish_connection(config)
ActiveRecord::Base.connection.drop_database config['database']
when /sqlite/
require 'pathname'
path = Pathname.new(config['database'])
file = path.absolute? ? path.to_s : File.join(Rails.root, path)
FileUtils.rm(file)
when /postgresql/
ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
ActiveRecord::Base.connection.select_all("select * from pg_stat_activity order by procpid;").each do |x|
if config['database'] == x['datname'] && x['current_query'] =~ /<IDLE>/
ActiveRecord::Base.connection.execute("select pg_terminate_backend(#{x['procpid']})")
end
end
ActiveRecord::Base.connection.drop_database config['database']
end
end
Run Code Online (Sandbox Code Playgroud)
这是一个适用于PostgreSQL 9.1和9.2 的修改版本.
小智 8
MacOS,如果 postgresql 是随brew一起安装的:
brew services restart postgresql
Run Code Online (Sandbox Code Playgroud)
乌本图,
首先检查一下(杀死在后台运行的服务器)
sudo kill -9 $(lsof -i :3000 -t)
Run Code Online (Sandbox Code Playgroud)
如果你没有找到 pid 那么你只需要通过命令重新启动 Postgresql 服务,如下所示:
sudo service postgresql restart
Run Code Online (Sandbox Code Playgroud)
小智 7
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
Run Code Online (Sandbox Code Playgroud)
我使用以下rake任务来覆盖Rails drop_database方法.
lib/database.rake
require 'active_record/connection_adapters/postgresql_adapter'
module ActiveRecord
module ConnectionAdapters
class PostgreSQLAdapter < AbstractAdapter
def drop_database(name)
raise "Nah, I won't drop the production database" if Rails.env.production?
execute <<-SQL
UPDATE pg_catalog.pg_database
SET datallowconn=false WHERE datname='#{name}'
SQL
execute <<-SQL
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = '#{name}';
SQL
execute "DROP DATABASE IF EXISTS #{quote_table_name(name)}"
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
编辑:这适用于Postgresql 9.2+
我是这样解决的:
在我的Windows8 64位中,只需restarting 服务:postgresql-x64-9.5
| 归档时间: |
|
| 查看次数: |
341459 次 |
| 最近记录: |