Skip to content

Instantly share code, notes, and snippets.

namespace :db do
desc 'kills connections to postgres db'
task :kill_postgres_connections => :environment do
puts "WARN: killing connections to #{ActiveRecord::Base.connection_config[:database]}."
ActiveRecord::Base.connection.execute("SELECT pid, pg_terminate_backend(pid) as terminated FROM pg_stat_activity WHERE pid <> pg_backend_pid()")
end
desc 'Alias for :kill_postgres_connections'
task :kill => :kill_postgres_connections