Skip to content

Commit

Permalink
psql script
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarton committed Jul 10, 2024
1 parent 2a0ef2d commit 08cbbd7
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions s/psql
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
ENVIRONMENT=local

while [[ $# -gt 0 ]]; do
case ${1} in
--environment)
ENVIRONMENT="${2}"
shift # past argument
shift # past value
;;
-*|--*)
echo "Unknown option ${1}"
exit 1
;;
esac
done

function connect_to_azure_db() {
local host="npda-$1.postgres.database.azure.com"
local user="NPDA\ Database\ Owners"
local dbname="$2"

local access_token=$(az account get-access-token --resource-type oss-rdbms --query 'accessToken' --output tsv)

PGPASSWORD="${access_token}" psql "host=${host} user=${user} dbname=${dbname} sslmode=require"
}

case ${ENVIRONMENT} in
'local')
docker compose exec postgis psql -U npdauser -d npdadb
;;

'staging')
connect_to_azure_db 'staging' 'npda_staging'
;;

'live')
connect_to_azure_db 'live' 'npda_live'
;;

*)
echo "Unknown environment ${ENVIRONMENT} - expected local, staging or live"
esac

0 comments on commit 08cbbd7

Please sign in to comment.