---
title: How to Create Azure SQL Database with PowerShell
description: Need to automate and create Azure SQL database and server? Look no further! You'll learn how to set up all things Azure SQL with PowerShell!
canonical: https://adamtheautomator.com/create-azure-sql-database/
---

# How to Create Azure SQL Database with PowerShell

> Need to automate and create Azure SQL database and server? Look no further! You'll learn how to set up all things Azure SQL with PowerShell!

Source: https://adamtheautomator.com/create-azure-sql-database/

---

- How to Create Azure SQL Database with PowerShell Tap to hide Home

- Tutorials

- Guidebooks

- Instructors

- Get Paid to Write

- Advertising

- Recommended Resources

- About Adam

                Search for:

-

-

-

-

# How to Create Azure SQL Database with PowerShell

        Published:25 May 2021 - 3 min. read

- Azure SQL
- Databases
- Microsoft Azure
- Microsoft SQL Server
- Powershell Administration

          [](https://adamtheautomator.com/author/gijs-reijn/)

            [Gijs Reijn](https://adamtheautomator.com/author/gijs-reijn/)

            Read [more tutorials](https://adamtheautomator.com/author/gijs-reijn/) by Gijs Reijn!

        [](https://specopssoft.com/product/specops-password-auditor/?utm_source=adamtheautomator&utm_medium=referral&utm_campaign=adamtheautomator_referral_na&utm_content=display)
        Audit Active Directory for stale users, weak passwords, and other security risks with [Specops Password Auditor](https://specopssoft.com/product/specops-password-auditor/?utm_source=adamtheautomator&utm_medium=referral&utm_campaign=adamtheautomator_referral_na&utm_content=text).

Table of Contents

- Prerequisites
- Creating the Azure SQL Server
- Creating the Azure SQL Server Firewall RuleHow to Create Azure SQL Database
- Connecting to the Azure SQL Database
- Wrapping Up
- Conclusion

                XFacebookLinkedIn

If you need to make changes to a SQL database, you could open SQL Server Management Studio, click around a little bit and make it happen. But what happens when you need to create an Azure SQL database 10 or 100 times or in some automation script? You need to use PowerShell!

Not a reader? Watch this related video tutorial!

 Not seeing the video? Make sure your ad blocker is disabled.

In this tutorial, you will learn how to create Azure SQL database and a SQL server firewall rule all in PowerShell!

Let's get going!

## Prerequisites

To follow along with the demos in this tutorial, be sure you have the following:

- A computer to run PowerShell – This tutorial uses Windows 10 using PowerShell v7.1.

- A code editor like Visual Studio (VS) Code.

- The Az PowerShell module – The tutorial will use v5.9.0.

- The dbatools PowerShell module – The tutorial will use v1.0.145.

- An Azure resource group – This tutorial will use a resource group called rg-dbaautomation in the westeurope region.

Related:[How to Rename/Move Azure Resource Groups (GUI and CLI)](https://adamtheautomator.com/rename-azure-resource-group/)

- You're authenticated to Azure in PowerShell.

## Creating the Azure SQL Server

Before you can create an Azure SQL database, you must create an Azure SQL server to host it on. Assuming you're already authenticated to Azure:

Open PowerShell on your local computer and create the [Azure SQL server](https://docs.microsoft.com/en-us/powershell/module/az.sql/new-azsqlserver?view=azps-5.9.0) that will host the Azure SQL database.

The command below is creating an Azure SQL server called sqlestate in the prerequisite resource group with a SQL admin username of SqlAdministrator and a password of AVeryStrongP@ssword0. The command is saving the output of the New-AzSqlServer cmdlet to use attributes from the server created later.

You can create a SQL admin username and password of your choosing as long as it meets the [database requirements](https://docs.microsoft.com/en-us/sql/relational-databases/security/password-policy?view=sql-server-ver15).

Related:[Using the PowerShell Get-Credential Cmdlet and All Things Credentials](https://adamtheautomator.com/powershell-get-credential/)

The SQL Server name must be globally unique.

## Convert the password to a secure string since creating a PSCredential
## object requires it
$pw = ConvertTo-SecureString -String 'AVeryStrongP@ssword0' -AsPlainText -Force

## Create the PSCredential object to pass to the New-AzSqlServer cmdlet
$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'SqlAdministrator',$pw

## Create the Azure SQL Server
$azSqlServer = New-AzSqlServer `
	-ServerName 'sqlestate123' `
	-ResourceGroupName 'rg-dbaautomation' `
	-Location 'westeurope' `
	-SqlAdministratorCredentials $credential

## Creating the Azure SQL Server Firewall Rule

By default, the Azure SQL server does not allow access to any outside entity. To connect to the server, you must next create a [server firewall rule](https://docs.microsoft.com/en-us/powershell/module/az.sql/new-azsqlserverfirewallrule?view=azps-5.9.0) to access the SQL server from your local machine.

To create the server firewall rule, find the public IP address of your local computer and invoke the [New-AzSqlServerFirewallRule cmdlet](https://docs.microsoft.com/en-us/powershell/module/az.sql/new-azsqlserverfirewallrule) to create the rule.

The command below is using the website [http://ipinfo.io](http://ipinfo.io) to find your computer's public IP address. It's then creating the firewall rule in the rg-dbaautomation resource group called FirewallRule_Access and specifying that IP address for the entire IP range.

## Find the local public IP address by querying a website
$ip = Invoke-RestMethod <https://ipinfo.io/json> | Select-Object -ExpandProperty IP

## Create the server firewall rule using the public IP address
New-AzSqlServerFirewallRule -ResourceGroupName 'rg-dbaautomation' -ServerName $azSqlServer.ServerName -FirewallRuleName 'FirewallRule_Access' -StartIpAddress $ip -EndIpAddress $ip

Creating the Azure SQL Server firewall rule

### How to Create Azure SQL Database

Finally, once you've created the Azure SQL server and firewall rule, now create the database using the [New-AzSqlDatabase cmdlet](https://docs.microsoft.com/en-us/powershell/module/az.sql/new-azsqldatabase).

The command below creates an Azure SQL database called Estate with only a Basic edition hosted on the server just created.

To find all available editions run the [Get-AzSqlServerServiceObjective PowerShell cmdlet](https://docs.microsoft.com/en-us/powershell/module/az.sql/get-azsqlserverserviceobjective?view=azps-5.9.0).

New-AzSqlDatabase -ResourceGroupName 'rg-dbaautomation' -ServerName $azSqlServer.ServerName -DatabaseName 'Estate' -Edition 'Basic'

Creating the Azure SQL database

### Connecting to the Azure SQL Database

You should now have an Azure SQL database running in your Azure subscription ready to work with. Now, confirm you can connect to it using the [Connect-DbaInstance PowerShell cmdlet](https://docs.dbatools.io/#Connect-DbaInstance).

Using the previously-created Azure SQL server's FQDN, the name of the database, and the SQL admin credential, test connection to the database using the code below.

## Test connecting to the instance
Connect-DbaInstance -SqlInstance $azSqlServer.FullyQualifiedDomainName -Database $azSqlDatabase.DatabaseName -SqlCredential $credential

If you can connect to the database, you will see the following output:

Running Connect-DbaInstance

### Wrapping Up

If you'd like to save all of these steps shown above into a single PowerShell script, create a new PowerShell script and copy and paste the below snippet.

$rg = New-AzResourceGroup -Name 'rg-dbaautomation' -Location 'westeurope'

## Convert the password to a secure string since creating a PSCredential
## object requires it
$pw = ConvertTo-SecureString -String 'AVeryStrongP@ssword0' -AsPlainText -Force

## Create the PSCredential object to pass to the New-AzSqlServer cmdlet
$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'SqlAdministrator',$pw

## Create the Azure SQL Server
$azSqlServer = New-AzSqlServer `
	-ServerName 'sqlestate123' `
	-ResourceGroupName $rg.ResourceGroupName `
	-Location $rg.Location `
	-SqlAdministratorCredentials $credential

## Find the local public IP address by querying a website
$ip = Invoke-RestMethod <https://ipinfo.io/json> | Select-Object -ExpandProperty IP

## Create the server firewall rule using the public IP address
New-AzSqlServerFirewallRule -ResourceGroupName $rg.ResourceGroupName -ServerName $azSqlServer.ServerName -FirewallRuleName 'FirewallRule_Access' -StartIpAddress $ip -EndIpAddress $ip

New-AzSqlDatabase -ResourceGroupName $rg.ResourceGroupName -ServerName $azSqlServer.ServerName -DatabaseName 'Estate' -Edition 'Basic'

## Test connecting to the instance
Connect-DbaInstance -SqlInstance $azSqlServer.FullyQualifiedDomainName -Database $azSqlDatabase.DatabaseName -SqlCredential $credential

## Conclusion

Using PowerShell to create an Azure SQL database makes the process much smoother than using the Azure Portal. PowerShell allows you to automate the process to quickly create Azure SQL servers and databases.

Where do you see your newfound ability to create Azure SQL databases with PowerShell fitting into your daily routine?

  Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

  [Explore ATA Guidebooks](https://adamtheautomator.com/ata-guidebooks/)

## More from ATA Learning and Partners

- ### Recommended Resources! Recommended Resources for Training, Information Security, Automation, and more!

- ### Get Paid to Write! ATA Learning is always seeking instructors of all experience levels. Regardless if you’re a junior admin or system architect, you have something to share. Why not write on a platform with an existing audience and share your knowledge with the world?

- ### ATA Learning Guidebooks ATA Learning is known for its high-quality written tutorials in the form of blog posts. Support ATA Learning with ATA Guidebook PDF eBooks available offline and with no ads!

## Categories

- IT Ops

- Cloud

- DevOps

- Home Ops

- Information Security

- Software Development

## Site

- Home

- Tutorials

- Guidebooks

- Instructors

- Get Paid to Write

- Advertising

- Recommended Resources

- About Adam

        Copyright 2026&copy; ATA Learning | [Privacy Policy](https://adamtheautomator.com/privacy/)

                                                        Don't be left behind with the ATA Learning Newsletter!

Looks like you're offline!
