zulip/scripts/setup/terminate-psql-sessions

32 lines
568 B
Plaintext
Raw Normal View History

#!/usr/bin/env bash
set -e
POSTGRES_USER="postgres"
if [ "$(uname)" = "OpenBSD" ]; then
POSTGRES_USER="_postgresql"
fi
cd /
username=$1
shift
tables="$(printf "'%s'," "${@//\'/\'\'}")"
tables="${tables%,}"
case "$(id -un)" in
root)
psql=(su -s /usr/bin/env - -- "$POSTGRES_USER" psql postgres "$POSTGRES_USER")
;;
"$POSTGRES_USER")
psql=(psql postgres "$POSTGRES_USER")
;;
*)
psql=(psql -h localhost postgres "$username")
;;
esac
"${psql[@]}" <<EOF
SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname IN ($tables);
EOF