Skip to content

Instantly share code, notes, and snippets.

@masonjm
Forked from mattscilipoti/postgres.rake
Last active August 29, 2015 14:02
Show Gist options
  • Save masonjm/5883b4eaa9a100d88103 to your computer and use it in GitHub Desktop.
Save masonjm/5883b4eaa9a100d88103 to your computer and use it in GitHub Desktop.
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
end
#
# This patch ensures db:drop works for postgres db, by killing the postgres connections
#
task 'db:drop' => 'db:kill_postgres_connections'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment