2016-01-12 13:08:43 +01:00
|
|
|
#!/usr/bin/env bash
|
2015-09-28 19:45:35 +02:00
|
|
|
set -e
|
|
|
|
|
2016-01-12 13:08:43 +01:00
|
|
|
DEFAULT_USER="postgres"
|
|
|
|
if [ "$(uname)" = "OpenBSD" ]; then
|
|
|
|
DEFAULT_USER="_postgresql"
|
|
|
|
fi
|
|
|
|
|
2015-09-28 19:45:35 +02:00
|
|
|
cd /
|
|
|
|
if [ "$EUID" -eq 0 ]; then
|
2016-01-12 13:08:43 +01:00
|
|
|
version=$(sudo -u "$DEFAULT_USER" sh -c 'psql -A -t -d postgres -c "show server_version"')
|
2015-09-28 19:45:35 +02:00
|
|
|
else
|
|
|
|
version=$(psql -A -t -d postgres -c "show server_version")
|
|
|
|
fi
|
|
|
|
|
2018-08-03 02:14:48 +02:00
|
|
|
major=$(echo "$version" | cut -d. -f1,2)
|
2015-09-28 19:45:35 +02:00
|
|
|
username=$1
|
2015-11-10 07:16:41 +01:00
|
|
|
|
2015-09-28 19:45:35 +02:00
|
|
|
shift
|
2018-08-03 02:14:48 +02:00
|
|
|
tables=$(echo "'$*'" | sed "s/ /','/g")
|
2015-09-28 19:45:35 +02:00
|
|
|
|
|
|
|
if [ "$EUID" -eq 0 ]; then
|
2016-02-18 02:48:20 +01:00
|
|
|
sudo -u "$DEFAULT_USER" sh -c "psql postgres '$DEFAULT_USER'" <<EOF
|
2018-08-10 00:24:22 +02:00
|
|
|
SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname IN ($tables);
|
2015-09-28 19:45:35 +02:00
|
|
|
EOF
|
|
|
|
else
|
|
|
|
psql -h localhost postgres "$username" <<EOF
|
2018-08-10 00:24:22 +02:00
|
|
|
SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname IN ($tables);
|
2015-09-28 19:45:35 +02:00
|
|
|
EOF
|
|
|
|
fi
|