Use PowerShell to create a single Azure SQL database and configure a firewall rule
This PowerShell script example creates an Azure SQL database and configures a server-level firewall rule. Once the script has been successfully run, the SQL Database can be accessed from all Azure services and the configured IP address.
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. Just click the Copy to copy the code, paste it into the Cloud Shell, and then press enter to run it. There are a few ways to launch the Cloud Shell:
|Click Try It in the upper right corner of a code block.|
|Open Cloud Shell in your browser.|
|Click the Cloud Shell button on the menu in the upper right of the Azure portal.|
If you choose to install and use the PowerShell locally, this tutorial requires the Azure PowerShell module version 5.7.0 or later. Run
Get-Module -ListAvailable AzureRM to find the version. If you need to upgrade, see Install Azure PowerShell module. If you are running PowerShell locally, you also need to run
Connect-AzureRmAccount to create a connection with Azure.
# Login-AzureRmAccount # Set the resource group name and location for your server $resourcegroupname = "myResourceGroup-$(Get-Random)" $location = "southcentralus" # Set an admin login and password for your server $adminlogin = "ServerAdmin" $password = "ChangeYourAdminPassword1" # Set server name - the logical server name has to be unique in the system $servername = "server-$(Get-Random)" # The sample database name $databasename = "mySampleDatabase" # The ip address range that you want to allow to access your server $startip = "0.0.0.0" $endip = "0.0.0.0" # Create a resource group $resourcegroup = New-AzureRmResourceGroup -Name $resourcegroupname -Location $location # Create a server with a system wide unique server name $server = New-AzureRmSqlServer -ResourceGroupName $resourcegroupname ` -ServerName $servername ` -Location $location ` -SqlAdministratorCredentials $(New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $adminlogin, $(ConvertTo-SecureString -String $password -AsPlainText -Force)) # Create a server firewall rule that allows access from the specified IP range $serverfirewallrule = New-AzureRmSqlServerFirewallRule -ResourceGroupName $resourcegroupname ` -ServerName $servername ` -FirewallRuleName "AllowedIPs" -StartIpAddress $startip -EndIpAddress $endip # Create a blank database with an S0 performance level $database = New-AzureRmSqlDatabase -ResourceGroupName $resourcegroupname ` -ServerName $servername ` -DatabaseName $databasename ` -RequestedServiceObjectiveName "S0" ` -SampleName "AdventureWorksLT" # Clean up deployment # Remove-AzureRmResourceGroup -ResourceGroupName $resourcegroupname
Clean up deployment
After the script sample has been run, the following command can be used to remove the resource group and all resources associated with it.
Remove-AzureRmResourceGroup -ResourceGroupName $resourcegroupname
This script uses the following commands. Each command in the table links to command specific documentation.
|New-AzureRmResourceGroup||Creates a resource group in which all resources are stored.|
|New-AzureRmSqlServer||Creates a logical server that hosts a database or elastic pool.|
|New-AzureRmSqlServerFirewallRule||Creates a firewall rule to allow access to all SQL Databases on the server from the entered IP address range.|
|New-AzureRmSqlDatabase||Creates a database in a logical server as a single or a pooled database.|
|Remove-AzureRmResourceGroup||Deletes a resource group including all nested resources.|
For more information on the Azure PowerShell, see Azure PowerShell documentation.
Additional SQL Database PowerShell script samples can be found in the Azure SQL Database PowerShell scripts.