Created
February 23, 2017 08:29
-
-
Save harunyasar/8e7fd887aa5c7c7d3fed238049a21d65 to your computer and use it in GitHub Desktop.
Kill all PostgreSQL connections
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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