terminate-psql-sessions: Fix shell and SQL quoting.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
This commit is contained in:
Anders Kaseorg 2019-01-15 19:32:27 -08:00 committed by Tim Abbott
parent c0792808e2
commit 981f09d950
1 changed files with 3 additions and 2 deletions

View File

@ -11,10 +11,11 @@ cd /
username=$1 username=$1
shift shift
tables=$(echo "'$*'" | sed "s/ /','/g") tables="$(printf "'%s'," "${@//\'/\'\'}")"
tables="${tables%,}"
if [ "$EUID" -eq 0 ]; then if [ "$EUID" -eq 0 ]; then
sudo -u "$DEFAULT_USER" sh -c "psql postgres '$DEFAULT_USER'" <<EOF sudo -u "$DEFAULT_USER" psql postgres "$DEFAULT_USER" <<EOF
SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname IN ($tables); SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname IN ($tables);
EOF EOF
else else