-
Notifications
You must be signed in to change notification settings - Fork 2
/
set-postgres-owner.bash
executable file
·43 lines (36 loc) · 1.1 KB
/
set-postgres-owner.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env bash
export PGPORT=5432
export AZURE_CONFIG_DIR=~/.azure-db-manager
az login --identity
## Delay until DB DNS and propagated
COUNT=0;
MAX=10;
while true; do
ping -c 1 $DB_HOST_NAME &>/dev/null
if [[ $? -eq 0 ]]; then
break
fi
if [[ $COUNT -eq $MAX ]]; then
break
else
COUNT=$[$COUNT+1]
fi
sleep 5
done
SINGLE_SERVER_USER=$(az keyvault secret show --vault-name "${KV_NAME}" --name "${USER_SECRET_NAME}" --subscription "${KV_SUBSCRIPTION}" --query value -o tsv)
SINGLE_SERVER_PASS=$(az keyvault secret show --vault-name "${KV_NAME}" --name "${PASS_SECRET_NAME}" --subscription "${KV_SUBSCRIPTION}" --query value -o tsv)
export PGPASSWORD=$SINGLE_SERVER_PASS
if [[ $SINGLE_SERVER_USER == *'@'* ]]; then
SINGLE_SERVER_USER="${SINGLE_SERVER_USER%%@*}"
fi
SQL_COMMAND="
GRANT ${DB_ADMIN} to ${SINGLE_SERVER_USER};
REASSIGN OWNED BY ${SINGLE_SERVER_USER} TO ${DB_ADMIN};
REVOKE ${DB_ADMIN} FROM ${SINGLE_SERVER_USER};
GRANT ${SINGLE_SERVER_USER} TO ${DB_ADMIN};
"
set -x
export PGDATABASE="${DB_NAME}"
export PGUSER="${SINGLE_SERVER_USER}"
psql -c "${SQL_COMMAND}"
set +x