---
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/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![How to Create Azure SQL Database with PowerShell](https://adamtheautomator.com/wp-content/uploads/2021/05/How-to-Create-an-Azure-SQL-Database-with-PowerShell.jpg)

# How to Create Azure SQL Database with PowerShell

[![](https://secure.gravatar.com/avatar/33c241a6b690cdbe352f5d33a308aad0e37d723a74e5d46c42ed6fd7a93e93f3?s=192&d=mm&r=g)Gijs Reijn](https://adamtheautomator.com/author/gijs-reijn/)25 May 20213 min. read

Categories: [Cloud](/category/cloud/)

Tags:[Azure SQL](/tag/azure-sql/)[Databases](/tag/databases/)[Microsoft Azure](/tag/microsoft-azure/)[Microsoft SQL Server](/tag/microsoft-sql-server/)[Powershell Administration](/tag/powershell-administration/)

Table of Contents

*   [Prerequisites](#h-prerequisites)
*   [Creating the Azure SQL Server](#h-creating-the-azure-sql-server)
*   [Creating the Azure SQL Server Firewall Rule](#h-creating-the-azure-sql-server-firewall-rule)
*   [How to Create Azure SQL Database](#h-creating-the-azure-sql-database)
*   [Connecting to the Azure SQL Database](#h-connecting-to-the-azure-sql-database)
*   [Wrapping Up](#h-wrapping-up)
*   [Conclusion](#h-conclusion)

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](https://www.powershellgallery.com/packages/Az/5.9.0) PowerShell module – The tutorial will use v5.9.0.
*   The [dbatools](https://www.powershellgallery.com/packages/dbatools/1.0.145) PowerShell module – The tutorial will use v1.0.145.
*   An [Azure resource group](https://docs.microsoft.com/en-us/azure/azure-resource-manager/management/manage-resource-groups-portal) – This tutorial will use a [resource](https://adamtheautomator.com/rename-azure-resource-group/) 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](https://adamtheautomator.com/connect-azaccount/) 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._

```powershell
## 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.

```powershell
## 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](https://adamtheautomator.com/wp-content/uploads/2021/05/Untitled-50-2.png)

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)._

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

![Creating the Azure SQL database](https://adamtheautomator.com/wp-content/uploads/2021/05/Untitled-51-2.png)

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.

```powershell
## 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](https://adamtheautomator.com/wp-content/uploads/2021/05/Untitled-52-2.png)

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.

```powershell
$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?

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fcreate-azure-sql-database%2F&text=How%20to%20Create%20Azure%20SQL%20Database%20with%20PowerShell)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fcreate-azure-sql-database%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fcreate-azure-sql-database%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2019/09/connect-azure-sql-database.jpg)

### [Connect to Azure SQL Database: A Comprehensive Guide](/connect-to-azure-sql-database/)

Learn to connect to Azure SQL Database and use it alongside or instead of traditional SQL Server installations.

![](https://adamtheautomator.com/wp-content/uploads/publisher/3075d9c85b2b811696d7c3fb28215d5b/2632cbcaed949b31f4a9b4c4ea73d850076a5034cf55e309a5815dd451ab0388.webp)

### [Stop Scaling Azure SQL: Find Real Performance Issues](/azure-sql-performance-tuning/)

Use Azure's built-in diagnostics to find real performance bottlenecks in your Azure SQL Database instead of scaling up immediately.

![](https://adamtheautomator.com/wp-content/uploads/2026/07/featured_image-1.webp)

### [Microsoft Azure Certification Roadmap: Choose the Right Path](/azure-certification-roadmap-2/)

Choose the right Microsoft Azure certification for your career goals. Compare AZ-900, AZ-104, AZ-305, AZ-400, AZ-700, DP-700, study timelines, and 2026 retirements.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
