zulip/scripts/setup/terminate-psql-sessions

25 lines
517 B
Bash
Executable File

#!/usr/bin/env bash
set -e
DEFAULT_USER="postgres"
if [ "$(uname)" = "OpenBSD" ]; then
DEFAULT_USER="_postgresql"
fi
cd /
username=$1
shift
tables=$(echo "'$*'" | sed "s/ /','/g")
if [ "$EUID" -eq 0 ]; then
sudo -u "$DEFAULT_USER" sh -c "psql postgres '$DEFAULT_USER'" <<EOF
SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname IN ($tables);
EOF
else
psql -h localhost postgres "$username" <<EOF
SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname IN ($tables);
EOF
fi