Skip to content

Instantly share code, notes, and snippets.

@robconery
Created September 14, 2018 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robconery/f7785952bb46493a178a0f5dc7478425 to your computer and use it in GitHub Desktop.
Save robconery/f7785952bb46493a178a0f5dc7478425 to your computer and use it in GitHub Desktop.
Spin up an Azure PostgreSQL database
#!/bin/bash
USER=admin_$RANDOM #set this to whatever you like but it's not something that should be easy
PASS=$(uuidgen) #Again - whatever you like but keep it safe! Better to make it random
SERVERNAME=[YOUR SERVER NAME] #this has to be unique across azure
#resource group
RG=[YOUR RESOURCE GROUP] #you can create these if you need to by uncommenting the lines below
#service plan
SP=[YOUR SERVICE PLAN]
#This is for the firewall so you can login from outside
#find this by going to http://www.whatsmyip.org or Google "what's my ip"
IP=[YOUR REMOTE IP]
#The sku-name parameter value follows the convention {pricing tier}_{compute generation}_{vCores} as in the examples below:
# --sku-name B_Gen4_2 maps to Basic, Gen 4, and 2 vCores.
# --sku-name GP_Gen5_32 maps to General Purpose, Gen 5, and 32 vCores.
# --sku-name MO_Gen5_2 maps to Memory Optimized, Gen 5, and 2 vCores.
SKU=B_Gen4_1 #this is the cheapest one
# Create a resource group if you need it.
#az group create --location westus --name $RG
# Create an App Service plan in FREE tier if you need it.
#az appservice plan create --name $APPNAME --resource-group $RG --sku FREE
echo "Spinning up PostgreSQL $SERVERNAME in group $RG with plan $SP, Admin is $USER"
# Create a web app. I'm using the Node runtime here but
az postgres server create --resource-group $RG \
--name $SERVERNAME --location westus --admin-user $USER \
--admin-password $PASS --sku-name $SKU --version 10.0
echo "Popping a hole in firewall"
az postgres server firewall-rule create --resource-group $RG \
--server $SERVERNAME --name AllowMyIP \
--start-ip-address $IP --end-ip-address $IP
echo "Your connection string is postgres://$USER@$SERVERNAME:$PASS@$SERVERNAME.postgres.database.azure.com"
echo "You can connect to this instance using psql too:"
echo "psql -h $SERVERNAME.postgres.database.azure.com -p 5432 -U $USER@$SERVERNAME -d postgres"
echo ""
echo "Connecting... your password is $PASS (copy and paste below)"
psql -h $SERVERNAME.postgres.database.azure.com -p 5432 -U $USER@$SERVERNAME -d postgres
#when you're all done, you can wipe this out by deleting the resource group...
#az group delete --name=$RG
#or just deleting the server:
#az postgres server delete --name=$SERVER --resource-group=$RG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment