2016-01-12 13:08:43 +01:00
|
|
|
#!/usr/bin/env bash
|
2015-09-28 19:45:35 +02:00
|
|
|
set -e
|
|
|
|
|
2019-04-13 01:30:49 +02:00
|
|
|
POSTGRES_USER="postgres"
|
2016-01-12 13:08:43 +01:00
|
|
|
if [ "$(uname)" = "OpenBSD" ]; then
|
2019-04-13 01:30:49 +02:00
|
|
|
POSTGRES_USER="_postgresql"
|
2016-01-12 13:08:43 +01:00
|
|
|
fi
|
|
|
|
|
2015-09-28 19:45:35 +02:00
|
|
|
cd /
|
|
|
|
|
|
|
|
username=$1
|
2015-11-10 07:16:41 +01:00
|
|
|
|
2015-09-28 19:45:35 +02:00
|
|
|
shift
|
2019-01-16 04:32:27 +01:00
|
|
|
tables="$(printf "'%s'," "${@//\'/\'\'}")"
|
|
|
|
tables="${tables%,}"
|
2015-09-28 19:45:35 +02:00
|
|
|
|
2019-04-04 23:29:21 +02:00
|
|
|
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
|
|
|
|
|
2019-04-23 23:41:42 +02:00
|
|
|
"${psql[@]}" -v ON_ERROR_STOP=1 <<EOF
|
|
|
|
SELECT pg_terminate_backend(s.pid)
|
|
|
|
FROM pg_stat_activity s, pg_roles r
|
|
|
|
WHERE
|
|
|
|
s.datname IN ($tables)
|
|
|
|
AND r.rolname = CURRENT_USER
|
|
|
|
AND (s.usename = r.rolname OR r.rolsuper = 't');
|
2015-09-28 19:45:35 +02:00
|
|
|
EOF
|