Skip to content

Instantly share code, notes, and snippets.

@harunyasar
Created February 23, 2017 08:29
Show Gist options
  • Save harunyasar/8e7fd887aa5c7c7d3fed238049a21d65 to your computer and use it in GitHub Desktop.
Save harunyasar/8e7fd887aa5c7c7d3fed238049a21d65 to your computer and use it in GitHub Desktop.
Kill all PostgreSQL connections
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment