Quickstart: Create an Azure Database for PostgreSQL server by using the Azure CLI

APPLIES TO: Azure Database for PostgreSQL - Single Server

Important

Azure Database for PostgreSQL - Single Server is on the retirement path. We strongly recommend that you upgrade to Azure Database for PostgreSQL - Flexible Server. For more information about migrating to Azure Database for PostgreSQL - Flexible Server, see What's happening to Azure Database for PostgreSQL Single Server?.

This quickstart shows how to use Azure CLI commands to create a single Azure Database for PostgreSQL server in five minutes.

Tip

Consider using the simpler az postgres up Azure CLI command. Try out the quickstart.

If you don't have an Azure trail subscription, create a trial subscription before you begin.

Prerequisites

If you prefer to run CLI reference commands locally, install the Azure CLI. If you're running on Windows or macOS, consider running Azure CLI in a Docker container. For more information, see How to run the Azure CLI in a Docker container.

  • If you're using a local installation, sign in to the Azure CLI by using the az login command. To finish the authentication process, follow the steps displayed in your terminal. For other sign-in options, see Sign in with the Azure CLI.

  • When you're prompted, install the Azure CLI extension on first use. For more information about extensions, see Use extensions with the Azure CLI.

  • Run az version to find the version and dependent libraries that are installed. To upgrade to the latest version, run az upgrade.

Sign in to Azure

Use the following script to sign in using a different subscription, replacing <Subscription ID> with your Azure Subscription ID. If you don't have an Azure trail subscription, create a trial subscription before you begin.

az cloud set -n AzureChinaCloud
az login

subscription="<subscriptionId>" # add subscription here

az account set -s $subscription # ...or use 'az login'

For more information, see set active subscription or log in.

Set parameter values

The following values are used in subsequent commands to create the database and required resources. Server names need to be globally unique across all of Azure so the $RANDOM function is used to create the server name.

Change the location as appropriate for your environment. Replace 0.0.0.0 with the IP address range to match your specific environment. Use the public IP address of the computer you're using to restrict access to the server to only your IP address.

# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="China East 2"
resourceGroup="msdocs-postgresql-rg-$randomIdentifier"
tags="create-postgresql-server-and-firewall-rule"
server="msdocs-postgresql-server-$randomIdentifier"
sku="GP_Gen5_2"
login="azureuser"
password="Pa$$w0rD-$randomIdentifier"
# Specify appropriate IP address values for your environment
# to limit / allow access to the PostgreSQL server
startIp=0.0.0.0
endIp=0.0.0.0
echo "Using resource group $resourceGroup with login: $login, password: $password..."

Create a resource group

Create a resource group with the az group create command. An Azure resource group is a logical container into which Azure resources are deployed and managed. The following example creates a resource group named myResourceGroup in the chinaeast2 location:

# Create a resource group
echo "Creating $resourceGroup in $location..."
az group create --name $resourceGroup --location "$location" --tag $tag

Create a server

Create a server with the az postgres server create command.

# Create a PostgreSQL server in the resource group
# Name of a server maps to DNS name and is thus required to be globally unique in Azure.
echo "Creating $server in $location..."
az postgres server create --name $server --resource-group $resourceGroup --location "$location" --admin-user $login --admin-password $password --sku-name $sku

Note

  • The server name can contain only lowercase letters, numbers, and the hyphen (-) character. It must contain 3 to 63 characters. For more information, see Azure Database for PostgreSQL Naming Rules.
  • The user name for the admin user can't be azure_superuser, admin, administrator, root, guest, or public.
  • The password must contain 8 to 128 characters from three of the following categories: English uppercase letters, English lowercase letters, numbers, and non-alphanumeric characters.
  • For information about SKUs, see Azure Database for PostgreSQL pricing.

Important

Configure a server-based firewall rule

Create a firewall rule with the az postgres server firewall-rule create command to give your local environment access to connect to the server.

# Configure a firewall rule for the server 
echo "Configuring a firewall rule for $server for the IP address range of $startIp to $endIp"
az postgres server firewall-rule create --resource-group $resourceGroup --server $server --name AllowIps --start-ip-address $startIp --end-ip-address $endIp

Tip

If you don't know your IP address, go to WhatIsMyIPAddress.com to get it.

Note

To avoid connectivity issues, make sure your network's firewall allows port 5432. Azure Database for PostgreSQL servers use that port.

List server-based firewall rules

To list the existing server firewall rules, run the az postgres server firewall-rule list command.

# List firewall rules for the server
echo "List of server-based firewall rules for $server"
az postgres server firewall-rule list --resource-group $resourceGroup --server-name $server
# You may use the switch `--output table` for a more readable table format as the output.

The output lists the firewall rules, if any, by default in JSON format. You may use the switch --output table for a more readable table format as the output.

Get the connection information

To connect to your server, provide host information and access credentials.

az postgres server show --resource-group $resourceGroup --name $server

Make a note of the administratorLogin and fullyQualifiedDomainName values.

Connect to the Azure Database for PostgreSQL server by using psql

The psql client is a popular choice for connecting to PostgreSQL servers. You can connect to your server by using psql on your local environment if you have it available. An empty database, postgres, is automatically created with a new PostgreSQL server. You can use that database to connect with psql, as shown in the following code.

psql --host=<server_name>.postgres.database.chinacloudapi.cn --port=5432 --username=<admin_user>@<server_name> --dbname=postgres

Tip

If you prefer to use a URL path to connect to Postgres, URL encode the @ sign in the username with %40. For example, the connection string for psql would be:

psql postgresql://<admin_user>%40<server_name>@<server_name>.postgres.database.chinacloudapi.cn:5432/postgres

Clean up resources

Use the following command to remove the resource group and all resources associated with it using the az group delete command - unless you have an ongoing need for these resources. Some of these resources may take a while to create, as well as to delete.

az group delete --name $resourceGroup

Next steps