Skip to content

Instantly share code, notes, and snippets.

@marcelmorgan
Created January 24, 2013 14:35
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 marcelmorgan/4622372 to your computer and use it in GitHub Desktop.
Save marcelmorgan/4622372 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# kill all connections to the postgres server
if [ -n "$1" ] ; then
where="where pg_stat_activity.datname = '$1'"
echo "killing all connections to database '$1'"
else
echo "killing all connections to database"
fi
cat <<-EOF | psql -U postgres -d postgres
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
${where}
EOF
# extracted from http://stackoverflow.com/questions/5408156/how-to-drop-a-postgresql-database-if-there-are-active-connections-to-it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment