From dd31dcd13e000eade8e2d7d5a97661b8adddb9ae Mon Sep 17 00:00:00 2001 From: Dale Newby Date: Tue, 20 Aug 2024 10:44:08 -0400 Subject: [PATCH 1/2] Use DB owner to drop database Find and use the current owner of the database when dropping the database during a restore operation. --- application/restore.sh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/application/restore.sh b/application/restore.sh index 07627d8..d22fab4 100755 --- a/application/restore.sh +++ b/application/restore.sh @@ -24,8 +24,14 @@ if [ -z "${result}" ]; then message="Database "${DB_NAME}" on host "${DB_HOST}" does not exist." echo "${MYNAME}: INFO: ${message}" else + message="finding current owner of DB ${DB_NAME}" + echo "${MYNAME}: ${message}" + db_owner=$(psql --host=${DB_HOST} --username=${DB_ROOTUSER} --command='\list' | grep ${DB_NAME} | cut -d '|' -f 2 | sed -e 's/ *//') + message="Database owner is ${db_owner}" + echo "${MYNAME}: INFO: ${message}" + echo "${MYNAME}: deleting database ${DB_NAME}" - result=$(psql --host=${DB_HOST} --dbname=postgres --username=${DB_USER} --command="DROP DATABASE ${DB_NAME};") + result=$(psql --host=${DB_HOST} --dbname=postgres --username=${db_owner} --command="DROP DATABASE ${DB_NAME};") if [ "${result}" != "DROP DATABASE" ]; then message="Drop database command failed: ${result}" echo "${MYNAME}: FATAL: ${message}" From 6fd6a468c1c29c54ebddeeb221c8e41f84b66180 Mon Sep 17 00:00:00 2001 From: Dale Newby Date: Tue, 20 Aug 2024 13:42:03 -0400 Subject: [PATCH 2/2] Remove all spaces around DB owner name --- application/restore.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/restore.sh b/application/restore.sh index d22fab4..80720cb 100755 --- a/application/restore.sh +++ b/application/restore.sh @@ -26,7 +26,7 @@ if [ -z "${result}" ]; then else message="finding current owner of DB ${DB_NAME}" echo "${MYNAME}: ${message}" - db_owner=$(psql --host=${DB_HOST} --username=${DB_ROOTUSER} --command='\list' | grep ${DB_NAME} | cut -d '|' -f 2 | sed -e 's/ *//') + db_owner=$(psql --host=${DB_HOST} --username=${DB_ROOTUSER} --command='\list' | grep ${DB_NAME} | cut -d '|' -f 2 | sed -e 's/ *//g') message="Database owner is ${db_owner}" echo "${MYNAME}: INFO: ${message}"