---
title: "Connect to Azure SQL Database: A Comprehensive Guide"
description: "Learn to connect to Azure SQL Database and use it alongside or instead of traditional SQL Server installations."
canonical: "https://adamtheautomator.com/connect-to-azure-sql-database/"
---

# Connect to Azure SQL Database: A Comprehensive Guide

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

Source: https://adamtheautomator.com/connect-to-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/)

![Connect to Azure SQL Database: A Comprehensive Guide](https://adamtheautomator.com/wp-content/uploads/2019/09/connect-azure-sql-database.jpg)

# Connect to Azure SQL Database: A Comprehensive Guide

[![](https://secure.gravatar.com/avatar/5989a502f0009a732739a0b9015d53ea609ead5436ee9de6e7e662a34534bacd?s=192&d=mm&r=g)Chris Blackden](https://adamtheautomator.com/author/chris/)3 September 20195 min. read

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

Tags:[Azure SQL Database](/tag/azure-sql-database/)[Microsoft Azure](/tag/microsoft-azure/)[Microsoft SQL Server](/tag/microsoft-sql-server/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Configuring the Azure SQL Database](#configuring-the-azure-sql-database)
*   [Connecting to an Azure SQL Database with The Azure Portal](#connecting-to-an-azure-sql-database-with-the-azure-portal)
*   [Connecting to an Azure SQL Database with SQL Server Management Studio (SSMS)](#connecting-to-an-azure-sql-database-with-sql-server-management-studio-ssms-)
*   [Connecting to an Azure SQL Database with Visual Studio Code (Code)](#connecting-to-an-azure-sql-database-with-visual-studio-code-code-)
*   [Connecting to an Azure SQL Database with PowerShell](#connecting-to-an-azure-sql-database-with-powershell)
*   [Wrapping Up](#wrapping-up)
*   [Further Reading](#further-reading)

Azure SQL Database is a managed SQL database service that’s built so developers and admins don’t have to perform the tedious tasks of installing SQL server, or spending hours setting up failover clusters.

Microsoft Azure has taken the heavy lifting out of setting up the database tier, so all that’s left to do is connect to it, so that’s what this article is going to cover. Here are several different ways to connect to an Azure SQL database.

## Prerequisites

If you’d like to connect to Azure SQL database, you’ll need to meet a few prerequisites ahead of time.

1.  An Azure account, or access to one
2.  An Azure SQL Database (You can use your own if you like, but I’ll be using the [sample](https://docs.microsoft.com/en-us/azure/sql-database/sql-database-single-database-get-started?tabs=azure-portal.).
3.  A Windows 10, Mac OS, or Linux laptop or desktop with PowerShell, Visual Studio Code and SQL Server Management Studio installed. The screenshots are all from Windows 10.

There are [dozens of different ways to connect to Azure SQL](https://docs.microsoft.com/en-us/azure/sql-database/sql-database-connect-query), and unfortunately I can’t go over them all, but the ones I will be covering in this post are:

*   The Azure Portal
*   SQL Server Management Studio
*   Visual Studio Code
*   PowerShell

## Configuring the Azure SQL Database

Before you get started, you must make sure you have a database available and can connect to it. Using the instructions from the [quickstart](https://docs.microsoft.com/en-us/azure/sql-database/sql-database-single-database-get-started?tabs=azure-portal.) document, I’ve set up a sample database called _ata-demo_. This database has a SQL admin (sa) user _AtaAdmin_ and the password is _Admin123_.

The password I’m using isn’t the most secure, but it’ll do for demo purposes. For the love of Equifax, please use best practices for passwords when you set something like this up in QA or Production!

Next, you’ll need to login to the [Azure Portal](https://portal.azure.com) to make sure that your device can to connect to the database. You can connect this to an existing Azure virtual network, or define an IP range that can access the database, which is what is done in the demos below. Go to the resource page of the SQL Database you want to connect to and look for an option called _Set Server Firewall_.

![Setting an Azure SQL Server firewall rule](https://adamtheautomator.com/wp-content/uploads/2020/06/Database-you-want-to-connect-to-and-look-for-an-option-called-Set-Server-Firewall.-1024x576.png)

Setting an Azure SQL Server firewall rule

Once here, you’ll have the option to choose an IP range. This page also shows your device’s IP address, which is what I’ll be using for the demo. But in practice, this will be the subnet range or virtual network of your application tier. Once that’s done, you’re ready to move on to the first demo!

## Connecting to an Azure SQL Database with The Azure Portal

Since you’re already on the Azure Portal, you can stay right here for the first demo.

Go back to the _Overview_ page using the navigation menu on the left and look for _Query Editor_. Once you’re there, you’ll be prompted to login with a Microsoft account or SQL Credentials. Use the SA account, you set up earlier to login (_AtaAdmin:_Admin123_)._

Once you have logged in, you’ll see a panel with the _Tables, Views, and Stored Procedures_ on the left of _Query Editor_. As of the time of this writing, there isn’t any right-click functionality in this UI, so your only option to interact with the database is through the _Query Editor_. If you’re using the sample database, you can use the sample query below.

```sql
SELECT pc.Name as CategoryName, p.name as ProductName
FROM [SalesLT].[ProductCategory] pc
JOIN [SalesLT].[Product] p
ON pc.productcategoryid = p.productcategoryid;
```

![Querying the Azure SQL server database](https://adamtheautomator.com/wp-content/uploads/2020/06/Now-that-you-know-the-firewall-rule-1024x575.jpg)

Querying the Azure SQL server database

Now that you know the firewall rules have been setup, we can start trying this from other platforms.

## Connecting to an Azure SQL Database with SQL Server Management Studio (SSMS)

By far the most robust tool for managing a SQL Database server is SSMS. Time-tested and battle-hardened, this has been the tool of choice for SQL server database administrators for over a decade.

SSMS is also fully compatible with Azure SQL databases. If you don’t have it installed, you can [download the latest version from Microsoft](https://docs.microsoft.com/en-us/sql/ssms/download-sql-server-management-studio-ssms?view=sql-server-ver15) or [install it using a package manager](https://chocolatey.org/packages/sql-server-management-studio).

Go back to your _SQL Database_ resource page and copy the _Server Name_. The server name will be _<Your Database Name>.database.windows.net_.

Open SQL Server Management Studio to connect to the database name using SQL Server authentication and the SA user you set up earlier. If you haven’t setup the firewall rule already, you will be prompted by SSMS to authenticate with your Azure account.

Now that you have authenticated to the database and the firewall is set up, you can start running your queries by going to _<ServerName>_ –> _Databases_, right-clicking on your database, and selecting _New Query_. Now run the query you used earlier and watch the magic happen.

![Connecting to an Azure SQL Database with SSMS](https://adamtheautomator.com/wp-content/uploads/2020/06/connect-azure-sql-database-ssms-1024x559.jpg)

Connecting to an Azure SQL Database with SSMS

## Connecting to an Azure SQL Database with Visual Studio Code (Code)

To connect to an Azure SQL database with Visual Studio Code, you’ll need to install the [mssql extension for Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=ms-mssql.mssql). Once installed, press _Ctrl + Shift + P_ to open the dialog box and search for _MS SQL: Connect_.

![Using the MSSql extension in Visual Studio Code](https://adamtheautomator.com/wp-content/uploads/2020/06/vs-code-azure-sql-1024x555.jpg)

Using the MSSql extension in Visual Studio Code

Once you select that, Visual Studio Code will run you through a wizard prompting you to enter the information of the database to connect to. You’ll be prompted for the server name, database name, username and password. Once you supply all this, it will be saved into a connection profile which can be used to execute queries against that database.

Input a SQL query to run against your database in the editor window. With the query open, press _Ctrl + Shift + E_. You will be prompted for which connection you want to run that query with. Choose the one you created and Code will open another panel with the results without ever leaving the screen!

![Connecting to mySql in Visual Studio Code](https://adamtheautomator.com/wp-content/uploads/2020/06/vs-code-sql-query.jpg)

Connecting to mySql in Visual Studio Code

## Connecting to an Azure SQL Database with PowerShell

To connect to an Azure SQL database with PowerShell, you’ll need to install the _Az_ [PowerShell module](https://adamtheautomator.com/powershell-modules/ "PowerShell module"). You can find this module on the PowerShell Gallery. Download and install the module by running the command `Install-Module Az`. At the time of this writing, the module is on version 2.5

The module only needs to be installed once on the machine you want to run your script from. Next, authenticate to Azure by running `Connect-AzAccount`. Learn about how to use the [`Connect-AzAccount`](https://adamtheautomator.com/connect-azaccount/) cmdlet, if you’d like to know more.

To verify everything works, you can find the database by running `Get-AzSqlServer -ResourceGroupName <Your Resource Group>`. If that returns the database you wanted to connect to, you are ready to run the script.

Now that you have authenticated and can see the database, you can query it by running the script below. This code will import the Azure SQL module, set up the parameters (database, username, credentials and query) and run `Invoke-SqlCmd` to initiate the transact SQL query.

```powershell
# Import the module
Import-Module Az.Sql -Force
 
# Setup your parameters
$Params = @{
   'ServerInstance' = 'atademo.database.windows.net';
   'Database' = 'ata-demo';
   'Username' = 'ataadmin';
   'Password' = 'Admin123';
   'Query' = 'SELECT pc.Name as CategoryName, p.name as ProductName
FROM [SalesLT].[ProductCategory] pc
JOIN [SalesLT].[Product] p
ON pc.productcategoryid = p.productcategoryid;'
}
 
# Splat
Invoke-Sqlcmd @Params
```

You can also use PowerShell to read the Azure SQL server and database and use that information to pass to the query as seen below. Assuming that you have `$rgName` defined and are passing the password as a [secure string](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.security/convertto-securestring?view=powershell-6), there isn’t a functional difference between the first script and the second.

```powershell
Import-Module Az.Sql -Force
 
$rgName = '<Resource Group Name>'
$sqlServer = Get-AzSqlServer -ResourceGroupName $rgName
$sqlDatabase = Get-AzSqlDatabase -ServerName $sqlServer.ServerName -ResourceGroupName $rgName
 
$Params = @{
   'ServerInstance' = $sqlServer.FullyQualifiedDomainName;
   'Database' = $sqlDatabase.DatabaseName[0];
   'Username' = $sqlServer.SqlAdministratorLogin;
   'Password' = ‘<Password>’;
   'Query' = 'SELECT pc.Name as CategoryName, p.name as ProductName
       FROM [SalesLT].[ProductCategory] pc
       JOIN [SalesLT].[Product] p
       ON pc.productcategoryid = p.productcategoryid;'
}
 
Invoke-Sqlcmd @Params
```

## Wrapping Up

Now that you’ve connected to an Azure SQL Database a few different ways, you should be comfortable using it either with or instead of a traditional SQL Server installation!

## Further Reading

*   [**_How to connect to SQL Server on Linux from Python_**](https://adamtheautomator.com/connect-to-sql-server-from-linux/)
*   [**_Azure SQL Documentation_**](https://docs.microsoft.com/en-us/azure/sql-database/)

Share this article

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

## Related Posts

![](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](/create-azure-sql-database/)

Need to automate creating an Azure SQL database and server? Look no further! You’ll learn how to set up all things Azure SQL with PowerShell!

![](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/)
