Created
September 25, 2012 10:56
-
-
Save gavinhughes/3781154 to your computer and use it in GitHub Desktop.
Rake task to disconnect postgres db
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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