From 68b526d1763590803a6399ddfc14be0838f16c37 Mon Sep 17 00:00:00 2001 From: "Nikita P. Shupeyko" Date: Mon, 30 Apr 2018 21:58:50 +0300 Subject: [PATCH] Export PG* envs when restoring postgres from backup --- .../production/postgres/maintenance/restore | 20 ++++++------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/{{cookiecutter.project_slug}}/compose/production/postgres/maintenance/restore b/{{cookiecutter.project_slug}}/compose/production/postgres/maintenance/restore index 498140073..eacca1ee4 100644 --- a/{{cookiecutter.project_slug}}/compose/production/postgres/maintenance/restore +++ b/{{cookiecutter.project_slug}}/compose/production/postgres/maintenance/restore @@ -37,7 +37,10 @@ if [[ "${POSTGRES_USER}" == "postgres" ]]; then exit 1 fi +export PGHOST="postgres" +export PGUSER="${POSTGRES_USER}" export PGPASSWORD="${POSTGRES_PASSWORD}" +export PGDATABASE="${POSTGRES_DB}" message_info "Dropping all connections to the database..." # Source: http://dba.stackexchange.com/a/11895 @@ -54,23 +57,12 @@ psql \ --command="${drop_postgres_connections_sql}" message_info "Dropping the database..." -dropdb \ - --host=postgres \ - --username="${POSTGRES_USER}" \ - "${POSTGRES_DB}" +dropdb "${PGDATABASE}" message_info "Creating a new database..." -createdb \ - --host=postgres \ - --username="${POSTGRES_USER}" \ - --owner="${POSTGRES_USER}" \ - "${POSTGRES_DB}" +createdb --owner="${POSTGRES_USER}" message_info "Applying the backup to the new database..." -gunzip -c "${backup_filename}" \ - | psql \ - --host=postgres \ - --username="${POSTGRES_USER}" \ - "${POSTGRES_DB}" +gunzip -c "${backup_filename}" | psql "${POSTGRES_DB}" message_success "The '${POSTGRES_DB}' database has been restored from the '${backup_filename}' backup."