Skip to content

Instantly share code, notes, and snippets.

@jeremyhaile
Created February 1, 2012 19:16
Show Gist options
  • Save jeremyhaile/1718726 to your computer and use it in GitHub Desktop.
Save jeremyhaile/1718726 to your computer and use it in GitHub Desktop.
Kill Postgres connections prior to rake db:drop
task :kill_postgres_connections => :environment do
db_name = ActiveRecord::Base.configurations[Rails.env.to_s]['database']
raise "No database name found in #{Rails.env} configuration." if db_name.nil?
sh = <<EOF
ps xa \
| grep postgres: \
| grep #{db_name} \
| grep -v grep \
| awk '{print $1}' \
| xargs kill
EOF
puts `#{sh}`
end
task "db:drop" => :kill_postgres_connections
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment