PowerShell is used to create and manage Azure resources from the command line or in scripts. This guide details using PowerShell to deploy an Azure SQL database in an Azure resource group in an Azure SQL Database logical server.
If you don't have an Azure subscription, create a free account before you begin.
This tutorial requires the Azure PowerShell module version 4.0 or later. Run
Get-Module -ListAvailable AzureRM to find the version. If you need to install or upgrade, see Install Azure PowerShell module.
Log in to Azure
Log in to your Azure subscription using the Add-AzureRmAccount command and follow the on-screen directions.
Define variables for use in the scripts in this quick start.
# The data center and resource name for your resources $resourcegroupname = "myResourceGroup" $location = "WestEurope" # The logical server name: Use a random value or replace with your own value (do not capitalize) $servername = "server-$(Get-Random)" # Set an admin login and password for your database # The login information for the server $adminlogin = "ServerAdmin" $password = "ChangeYourAdminPassword1" # The ip address range that you want to allow to access your server - change as appropriate $startip = "0.0.0.0" $endip = "0.0.0.0" # The database name $databasename = "mySampleDatabase"
Create a resource group
Create an Azure resource group using the New-AzureRmResourceGroup command. A resource group is a logical container into which Azure resources are deployed and managed as a group. The following example creates a resource group named
myResourceGroup in the
New-AzureRmResourceGroup -Name $resourcegroupname -Location $location
Create a logical server
Create an Azure SQL Database logical server using the New-AzureRmSqlServer command. A logical server contains a group of databases managed as a group. The following example creates a randomly named server in your resource group with an admin login named
ServerAdmin and a password of
ChangeYourAdminPassword1. Replace these pre-defined values as desired.
New-AzureRmSqlServer -ResourceGroupName $resourcegroupname ` -ServerName $servername ` -Location $location ` -SqlAdministratorCredentials $(New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $adminlogin, $(ConvertTo-SecureString -String $password -AsPlainText -Force))
Configure a server firewall rule
Create an Azure SQL Database server-level firewall rule using the New-AzureRmSqlServerFirewallRule command. A server-level firewall rule allows an external application, such as SQL Server Management Studio or the SQLCMD utility to connect to a SQL database through the SQL Database service firewall. In the following example, the firewall is only opened for other Azure resources. To enable external connectivity, change the IP address to an appropriate address for your environment. To open all IP addresses, use 0.0.0.0 as the starting IP address and 255.255.255.255 as the ending address.
New-AzureRmSqlServerFirewallRule -ResourceGroupName $resourcegroupname ` -ServerName $servername ` -FirewallRuleName "AllowSome" -StartIpAddress $startip -EndIpAddress $endip
SQL Database communicates over port 1433. If you are trying to connect from within a corporate network, outbound traffic over port 1433 may not be allowed by your network's firewall. If so, you will not be able to connect to your Azure SQL Database server unless your IT department opens port 1433.
Create a database in the server with sample data
Create a database with an S0 performance level in the server using the New-AzureRmSqlDatabase command. The following example creates a database called
mySampleDatabase and loads the AdventureWorksLT sample data into this database. Replace these predefined values as desired (other quick starts in this collection build upon the values in this quick start).
New-AzureRmSqlDatabase -ResourceGroupName $resourcegroupname ` -ServerName $servername ` -DatabaseName $databasename ` -SampleName "AdventureWorksLT" ` -RequestedServiceObjectiveName "S0"
Clean up resources
Other quick starts in this collection build upon this quick start.
If you plan to continue on to work with subsequent quick starts, do not clean up the resources created in this quick start. If you do not plan to continue, use the following steps to delete all resources created by this quick start in the Azure portal.
Remove-AzureRmResourceGroup -ResourceGroupName $resourcegroupname
Now that you have a database, you can connect and query using your favorite tools. Learn more by choosing your tool below: