Skip to content

Instantly share code, notes, and snippets.

@iamkristian
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamkristian/55c89d7cc0b00ae10187 to your computer and use it in GitHub Desktop.
Save iamkristian/55c89d7cc0b00ae10187 to your computer and use it in GitHub Desktop.
Kill postgres connections upon rake db:drop
# lib/tasks/kill_postgres_connections.rake
desc "Kills postgres connections"
task :kill_postgres_connections => :environment do
yml_file = Rails.root.join('config/database.yml')
db_settings = YAML::load(File.open(yml_file))[Rails.env]
db_name = db_settings['database']
sh = <<EOF
ps -e |
grep postgres: |
grep #{db_name} |
cut -d ':' -f 3 |
awk '{print $1}' |
xargs kill
EOF
puts "Killing db connections to #{db_name}"
`#{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