共用方式為


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 in Azure Cloud Shell 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 subscription, create an Azure free account before you begin.

Prerequisites

Launch Azure Cloud Shell

The Azure Cloud Shell is a free interactive shell that you can use to run the steps in this article. It has common Azure tools preinstalled and configured to use with your account.

To open the Cloud Shell, just select Try it from the upper right corner of a code block. You can also launch Cloud Shell in a separate browser tab by going to https://shell.azure.com.

When Cloud Shell opens, verify that Bash is selected for your environment. Subsequent sessions will use Azure CLI in a Bash environment, Select Copy to copy the blocks of code, paste it into the Cloud Shell, and press Enter to run it.

Sign in to Azure

Cloud Shell is automatically authenticated under the initial account signed-in with. 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 subscription, create an Azure free account before you begin.

subscription="<subscriptionId>" # add subscription here

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

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

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="East US"
resourceGroup="msdocs-postgresql-rg-$randomIdentifier"
tag="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 eastus location:

# Create a resource group
echo "Creating $resourceGroup in $location..."
az group create --name $resourceGroup --location "$location" --tags $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 with Azure Cloud Shell. You can also use 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.azure.com --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.azure.com: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