Azure CLI samples for Azure SQL Database and SQL Managed Instance

APPLIES TO: Azure SQL Database Azure SQL Managed Instance

You can configure Azure SQL Database and SQL Managed Instance by using the Azure CLI.

If you don't have an Azure subscription, create a free account before you begin.

Prerequisites

  • Use the Bash environment in Azure Cloud Shell.

    Launch Cloud Shell in a new window

  • If you prefer, install the Azure CLI to run CLI reference commands.

    • 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 additional sign-in options, see Sign in with the Azure CLI.

    • When you're prompted, install Azure CLI extensions 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.

  • This tutorial requires version 2.0 or later of the Azure CLI. If using Azure Cloud Shell, the latest version is already installed.

The following table includes links to Azure CLI script examples to manage single and pooled databases in Azure SQL Database.

Area Description
Create databases in Azure SQL Database
Create a single database and configure a firewall rule Creates an SQL Database and configures a server-level firewall rule.
Create elastic pools and move pooled databases Creates elastic pools, moves pooled databases, and changes compute sizes.
Scale databases in Azure SQL Database
Scale a single database Scales a database in SQL Database to a different compute size after querying the size information for the database.
Scale an elastic pool Scales a SQL elastic pool to a different compute size.
Configure geo-replication and failover
Add a single database to a failover group Creates a database and a failover group, adds the database to the failover group, then tests failover to the secondary server.
Configure a failover group for an elastic pool Creates a database, adds it to an elastic pool, adds the elastic pool to the failover group, then tests failover to the secondary server.
Configure and fail over a single database by using active geo-replication Configures active geo-replication for a database in Azure SQL Database and fails it over to the secondary replica.
Configure and fail over a pooled database by using active geo-replication Configures active geo-replication for a database in an elastic pool, then fails it over to the secondary replica.
Auditing and threat detection
Configure auditing and threat-detection Configures auditing and threat detection policies for a database in Azure SQL Database.
Back up, restore, copy, and import a database
Back up a database Backs up a database in SQL Database to an Azure storage backup.
Restore a database Restores a database in SQL Database from a geo-redundant backup and restores a deleted database to the latest backup.
Copy a database to a new server Creates a copy of an existing database in SQL Database in a new server.
Import a database from a BACPAC file Imports a database to SQL Database from a BACPAC file.

Learn more about the single-database Azure CLI API.