杀死一个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. 
 我尝试closures从ps -ef | grep postgres看到的进程  ps -ef | grep postgres但是这也不起作用: 
 kill: kill 2358 failed: operation not permitted 
	
您可以使用pg_terminate_backend()来终止连接。 您必须是超级用户才能使用此function。 这适用于所有操作系统。
 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' ; 
执行此查询之前,您必须REVOKE CONNECT权限才能避免新的连接:
 REVOKE CONNECT ON DATABASE dbname FROM PUBLIC, username; 
如果您使用的是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' ; 
 也许只是重新启动postgres => sudo service postgresql restart 
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 
 如果你的datadir在其他地方,你可以通过检查ps aux | grep postgres的输出来找出它的位置 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 
从这里和这里发现的要点中解脱出来。
这是一个适用于PostgreSQL 9.1和9.2 的修改版本 。
有关运行过程的所有信息:
 SELECT *, pg_terminate_backend(pid) FROM pg_stat_activity WHERE pid <> pg_backend_pid() AND datname = 'my_database_name'; 
 我使用下面的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 
编辑:这是Postgresql 9.2+
我有这个问题,问题是Navicat连接到我的本地Postgres数据库。 一旦我断开Navicat的问题消失。
编辑:
而且, 作为绝对最后的手段,您可以备份数据,然后运行以下命令:
 sudo kill -15 `ps -u postgres -o pid` 
  …将杀死所有postgres用户正在访问。 避免在生产机器上这样做,但不应该对开发环境有任何问题。 确保每个 postgres进程在尝试重新启动PostgreSQL之前确实已经终止,这一点至关重要。 
编辑2:
 由于这个unix.SE职位,我已经从kill -9变为kill -15 。 
我已经解决了这个问题:
 在我的Windows8 64位,只需restart服务: postgresql-x64-9.5 
只是想指出,如果其他后台进程使用数据库,哈里斯的答案可能无法正常工作,在我的情况下,这是延期工作,我做了:
 script/delayed_job stop 
只有这样我才能够删除/重置数据库。
退出postgres并重新启动它。 简单,但每次都适用于我,其他cli命令有时不会。
没有必要放弃它。 只需删除并重新创build公共架构。 在大多数情况下,这具有完全相同的效果。
 namespace :db do desc 'Clear the database' task :clear_db => :environment do |t,args| ActiveRecord::Base.establish_connection ActiveRecord::Base.connection.tables.each do |table| next if table == 'schema_migrations' ActiveRecord::Base.connection.execute("TRUNCATE #{table}") end end desc 'Delete all tables (but not the database)' task :drop_schema => :environment do |t,args| ActiveRecord::Base.establish_connection ActiveRecord::Base.connection.execute("DROP SCHEMA public CASCADE") ActiveRecord::Base.connection.execute("CREATE SCHEMA public") ActiveRecord::Base.connection.execute("GRANT ALL ON SCHEMA public TO postgres") ActiveRecord::Base.connection.execute("GRANT ALL ON SCHEMA public TO public") ActiveRecord::Base.connection.execute("COMMENT ON SCHEMA public IS 'standard public schema'") end desc 'Recreate the database and seed' task :redo_db => :environment do |t,args| # Executes the dependencies, but only once Rake::Task["db:drop_schema"].invoke Rake::Task["db:migrate"].invoke Rake::Task["db:migrate:status"].invoke Rake::Task["db:structure:dump"].invoke Rake::Task["db:seed"].invoke end end 
远程场景。 但是,如果你想在Rails应用程序中运行testing,并且得到类似的东西
“ActiveRecord :: StatementInvalid:PG :: ObjectInUse:ERROR:数据库”myapp_test“正在被其他用户访问细节:有1个其他会话使用数据库。
确保在运行testing之前closurespgAdmin或任何其他postgres GUI工具。
 案件 : 
 无法执行查询: 
 DROP TABLE dbo.t_tabelname 
 解答: 
 一个。 显示查询状态活动如下: 
 SELECT * FROM pg_stat_activity ; 
湾 查找“查询”列包含的行:
 'DROP TABLE dbo.t_tabelname' 
C。 在同一行中,获取“PID”列的值
 example : 16409 
d。 执行这些脚本:
 SELECT pg_terminate_backend(25263) FROM pg_stat_activity WHERE -- don't kill my own connection! 25263 <> pg_backend_pid() -- don't kill the connections to other databases AND datname = 'database_name' ;