Skip to content

Instantly share code, notes, and snippets.

@gavinhughes
Created September 25, 2012 10:56
Show Gist options
  • Save gavinhughes/3781154 to your computer and use it in GitHub Desktop.
Save gavinhughes/3781154 to your computer and use it in GitHub Desktop.
Rake task to disconnect postgres db
namespace :db do
desc "Drop postgresql db connections"
task :disconnect => :environment do
begin
ActiveRecord::Base.connection.select_all("select * from pg_stat_activity order by procpid;").each do |x|
ActiveRecord::Base.connection.execute("select pg_terminate_backend(#{x['procpid']})")
end
rescue
end
puts "DB sessions disconnected."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment