Skip to content

Instantly share code, notes, and snippets.

@mhenrixon
Last active December 22, 2019 12:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mhenrixon/4597838 to your computer and use it in GitHub Desktop.
Save mhenrixon/4597838 to your computer and use it in GitHub Desktop.
Kills all connections to postgresql (un)specified database(s)
#!/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
where="where pg_stat_activity.datname in (select datname from pg_database where datname != 'postgres')"
echo "killing all connections to database"
fi
cat <<-EOF | psql -U mhenrixon -d postgres
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
${where}
EOF
alias mgd="be rake db:migrate"
alias mgt="be rake db:test:prepare"
alias mg="mgd && mgt"
alias mgtrace="$mgd --trace && $mgt --trace"
alias mgr="$mgd\:redo && $mgt\:redo"
alias mgrtrace="$mgd:redo --trace && $mgt:redo --trace"
alias rbd="be rake db:rollback"
alias rbt="be rake db:rollback"
alias rb="rbd && rbt"
alias seed="be rake db:seed"
alias fuckdb="dropdb && createdb && mgd && be rake db:test:prepare && seed"
alias dropdb="~/pg_kill.sh && be rake db:drop"
alias createdb="be rake db:create"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment