From fa2496c229e123e67048ca1143522c6402034251 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Mon, 15 Jun 2020 16:36:02 -0700 Subject: [PATCH] terminate-psql-sessions: Rely on the caller to set PGHOST, PGUSER. Signed-off-by: Anders Kaseorg --- scripts/setup/postgres-init-db | 3 ++- scripts/setup/restore-backup | 4 ++-- scripts/setup/terminate-psql-sessions | 22 +--------------------- tools/rebuild-dev-database | 3 ++- tools/rebuild-test-database | 3 ++- tools/setup/generate-fixtures | 3 ++- tools/setup/postgres-init-dev-db | 3 ++- 7 files changed, 13 insertions(+), 28 deletions(-) diff --git a/scripts/setup/postgres-init-db b/scripts/setup/postgres-init-db index fcee219eb9..016349a50a 100755 --- a/scripts/setup/postgres-init-db +++ b/scripts/setup/postgres-init-db @@ -33,7 +33,8 @@ if [ -e "/var/run/supervisor.sock" ]; then fi # Drop any open connections to any old database. -"$(dirname "$0")/terminate-psql-sessions" postgres zulip zulip_base +su -s /usr/bin/env - -- "$POSTGRES_USER" \ + "$(dirname "$0")/terminate-psql-sessions" zulip zulip_base ( cd / # Make sure the current working directory is readable by postgres diff --git a/scripts/setup/restore-backup b/scripts/setup/restore-backup index 432cf4a61c..15446778ed 100755 --- a/scripts/setup/restore-backup +++ b/scripts/setup/restore-backup @@ -84,17 +84,17 @@ def restore_backup(tarball_file: IO[bytes]) -> None: db_dir = os.path.join(tmp, "zulip-backup", "database") os.setresuid(0, 0, 0) run(["chown", "-R", POSTGRES_USER, "--", tmp]) + as_postgres = ["su", "-s", "/usr/bin/env", "-", "--", POSTGRES_USER] run( [ + *as_postgres, os.path.join( settings.DEPLOY_ROOT, "scripts", "setup", "terminate-psql-sessions", ), "zulip", - "zulip", "zulip_base", ], ) - as_postgres = ["su", "-s", "/usr/bin/env", "-", "--", POSTGRES_USER] run(as_postgres + ["dropdb", "--if-exists", "--", db_name]) run(as_postgres + ["createdb", "-O", "zulip", "-T", "template0", "--", db_name]) diff --git a/scripts/setup/terminate-psql-sessions b/scripts/setup/terminate-psql-sessions index c6340d270c..f952eae14d 100755 --- a/scripts/setup/terminate-psql-sessions +++ b/scripts/setup/terminate-psql-sessions @@ -1,32 +1,12 @@ #!/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[@]}" -v ON_ERROR_STOP=1 <