Create Your First Azure SQL Database
Learn to create your first Azure SQL database in this Azure Tutorial lesson. Step-by-step instructions cover setup, configuration, and connectivity, with troubleshooting tips and what to study next.
Focus: create your first azure sql database
You've built applications, configured identity, and deployed containers — but where does your data actually live? Without a managed, scalable, and secure database, your Azure workloads are just stateless shells. This lesson walks you through creating your first Azure SQL Database, from provisioning to connecting with a real client, so you can start persisting data the way production systems do.
The problem this lesson solves
Every serious application needs a place to store data — user profiles, orders, logs, or telemetry. You could host a database on a VM, but then you are responsible for backups, patching, high availability, and security updates. That's a distraction from your actual product.
Azure SQL Database solves this by offering a fully managed relational database service. You don't manage the underlying operating system or infrastructure. Microsoft handles patching, backups, and replication. That's a massive win for developers and DevOps engineers who want to focus on application logic.
The pain point: many developers waste hours trying to connect to a database that doesn't exist yet, or they provision the wrong tier and overpay. This lesson eliminates that confusion by giving you a repeatable path to a working database and a verified connection.
Core concept / mental model
Think of Azure SQL Database as a car with a driver — you control the destination and the comfort, but a professional handles the engine mechanics. You choose the performance tier, configure networking, and manage your schema. Azure handles updates, patches, and automatic backups.
Here's the mental model:
- Logical server — a management container that holds your databases, similar to an on-premises SQL Server instance. It has a DNS name, firewall rules, and admin credentials.
- Single database — the actual database inside the logical server, with its own performance characteristics and storage.
- Connection string — the address and credentials your application uses to talk to the database.
In simpler terms: the logical server is the building, the database is an apartment inside, and the connection string is the key that lets your app in.
Pro tip: Don't confuse the logical server with a VM. You never RDP into it. You interact through the Azure portal, CLI, or direct database connections.
How it works step by step
The process of creating your first Azure SQL Database follows a logical order. Get these steps right and you'll avoid most common pitfalls.
Step 1: Create a resource group
A resource group is a logical container for related Azure resources. Put your server, database, and any other related resources in the same group for easier management and cost tracking.
Step 2: Provision the logical server
When you create a database, you'll also create a logical server (or reuse an existing one). The server needs:
- A globally unique DNS name
- Admin credentials (login and password)
- A region (choose one close to your users)
Step 3: Create the database
Choose your performance tier — Basic for learning, Standard for low-cost production, Premium for high throughput. Name your database and select a collation (default is fine for most cases).
Step 4: Configure networking
By default, Azure SQL Database blocks all external traffic. You must configure a firewall rule to allow your client IP address to connect. For production, use a Virtual Network rule or a private endpoint.
Step 5: Get the connection string
Azure generates connection strings for common clients (ADO.NET, JDBC, Python, PHP). Copy the one for your language and use it in your app.
Step 6: Connect from your code
Use your language's SQL client library to connect and run queries. We'll do this hands-on in the next section.
Pro tip: Always set a server-level firewall rule from your IP, not
0.0.0.0/0. The latter exposes your database to the entire internet.
Hands-on walkthrough
Let's create your first Azure SQL Database and connect to it. You can use the Azure portal or the CLI — the CLI is more reproducible, so we'll use it here.
Prerequisites
- An Azure subscription (free tier works)
- Azure CLI installed and logged in
1. Log in and set your subscription
az login
az account show --output table
If you have multiple subscriptions, select one:
az account set --subscription "your-subscription-id"
2. Create a resource group
az group create --name rg-sqldb-tutorial --location eastus
Expected output:
{
"id": "/subscriptions/.../resourceGroups/rg-sqldb-tutorial",
"location": "eastus",
"name": "rg-sqldb-tutorial",
"provisioningState": "Succeeded"
}
3. Create the logical server
az sql server create \
--name my-sqldb-server-2025 \
--resource-group rg-sqldb-tutorial \
--location eastus \
--admin-user sqladmin \
--admin-password 'YourStrongPass!1'
Security note: Store the admin password in Azure Key Vault or use Azure AD authentication in production. Never hardcode passwords in code.
Expected output: JSON with fullyQualifiedDomainName like my-sqldb-server-2025.database.windows.net.
4. Create the database
az sql db create \
--resource-group rg-sqldb-tutorial \
--server my-sqldb-server-2025 \
--name TodoDB \
--edition Basic \
--capacity 5
The Basic tier is perfect for learning and light workloads. Later you can scale up.
Expected output: JSON with status: Online after a short wait.
5. Configure the firewall
Get your public IP and allow it:
MY_IP=$(curl -s ifconfig.me)
az sql server firewall-rule create \
--resource-group rg-sqldb-tutorial \
--server my-sqldb-server-2025 \
--name AllowMyIP \
--start-ip-address $MY_IP \
--end-ip-address $MY_IP
6. Connect with Python
Install the pyodbc driver (or use pymssql). Here's how to connect and run a query:
import pyodbc
server = 'my-sqldb-server-2025.database.windows.net'
database = 'TodoDB'
username = 'sqladmin'
password = 'YourStrongPass!1'
driver = '{ODBC Driver 18 for SQL Server}'
# Connection string
conn_str = f'DRIVER={driver};SERVER={server};DATABASE={database};UID={username};PWD={password};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30'
conn = pyodbc.connect(conn_str)
cursor = conn.cursor()
# Create a table
cursor.execute('''
CREATE TABLE Todos (
Id INT PRIMARY KEY IDENTITY,
Title NVARCHAR(100) NOT NULL,
IsCompleted BIT NOT NULL DEFAULT 0
)
''')
conn.commit()
# Insert a row
cursor.execute("INSERT INTO Todos (Title) VALUES ('Learn Azure SQL')")
conn.commit()
# Query
cursor.execute('SELECT Id, Title, IsCompleted FROM Todos')
for row in cursor.fetchall():
print(row)
conn.close()
Expected output:
(1, 'Learn Azure SQL', False)
This proves your database is live and accessible.
7. Query via the CLI (optional)
You can also run SQL directly using sqlcmd:
sqlcmd -S my-sqldb-server-2025.database.windows.net -U sqladmin -P 'YourStrongPass!1' -d TodoDB -Q "SELECT @@VERSION"
Compare options / when to choose what
| Option | Best for | Pros | Cons |
|---|---|---|---|
| Azure SQL Database (single) | Small to medium apps | Fully managed, automatic backups, built-in intelligence | Cost scales with usage |
| Azure SQL Managed Instance | Large apps needing SQL Server features | Near 100% compatibility with SQL Server | More expensive, more management overhead |
| Azure SQL Edge | IoT and edge workloads | Optimized for edge devices | Limited feature set |
| Azure Database for PostgreSQL / MySQL | Open-source preference | Lower license cost | Not SQL Server-compatible |
For your first database, single database on Basic tier is the right choice. It's cheap, fully managed, and enough for learning. Move to Managed Instance only if you need features like SQL Agent or cross-database queries.
Pro tip: Use the serverless compute tier if your workload is intermittent — you pay per second of usage, not for idle time.
Troubleshooting & edge cases
"Cannot connect to the server"
This is the most common issue. Check:
- Firewall rules — your client IP might not be allowed. Re-run the firewall step or use Azure portal's "Add client IP" button.
- TLS/Encryption — modern drivers default to
Encrypt=yes. Make sure you use the latest ODBC driver. - Credentials — you must use the server admin login, not your subscription credentials.
"Login failed for user 'sqladmin'"
Double-check the username and password. Also, if you enabled Azure AD-only authentication, the SQL admin login won't work.
Database creation fails
Your region might have capacity issues or the database name might conflict. Try a different region or a more unique name.
Performance is slow
The Basic tier has limited IOPS. If your queries time out, consider scaling to a higher tier or optimizing your queries with indexes.
Edge case: connecting from Azure Functions
If your code runs inside Azure (like an Azure Function), you don't need to allow your IP. Instead, enable the "Allow Azure services" firewall rule or better, use a private endpoint.
What you learned & what's next
You now know how to create your first Azure SQL Database from scratch, connect to it using Python, and avoid the most common pitfalls. You understand the mental model of logical servers, databases, and connection strings, and you can compare Azure SQL options to pick the right one for your needs.
In the next lesson, we'll explore securing your database with Azure Active Directory authentication and managed identities — crucial for production deployments. You'll then move on to integrating your database with other Azure services like App Service and Functions.
Key takeaways from this lesson:
- Azure SQL Database is fully managed — no patching, backups, or OS maintenance.
- A logical server contains one or more databases; the server is the management boundary.
- The Basic tier is ideal for learning and small workloads.
- Always configure firewall rules carefully — never expose your database to the whole internet.
- Use the connection string generated by Azure with your language's SQL client.
- The CLI makes provisioning reproducible — script it for production.
You're now ready to build data-driven applications on Azure. Go create your database again, this time from memory, and then explore the next lesson on securing it.
Practice recap
Now that you've created your first Azure SQL Database, try creating a second one using the Azure portal to reinforce the steps. Then, write a Python script that connects to your database and performs a CRUD operation on a table you design. Compare both experiences and note which method you prefer for your workflow.
Common mistakes
- Forgetting to configure firewall rules — the database is unreachable by default.
- Using the free-tier database for production workloads — the Basic tier has limited IOPS.
- Hardcoding credentials in application code — use Azure Key Vault or Managed Identity.
- Choosing a Premium tier and overpaying when a Standard tier would suffice.
Variations
- Use the Azure portal instead of the CLI for a visual, click-through experience.
- Provision via Terraform or Bicep for infrastructure-as-code and repeatable deployments.
- Select Azure SQL Managed Instance if you need full SQL Server feature compatibility.
Real-world use cases
- A small web app needs a reliable, managed database without hiring a DBA.
- A microservices architecture uses Azure SQL for transactional data in each service.
- A startup wants a scalable relational store with automatic backups and geo-replication.
Key takeaways
- Azure SQL Database is a fully managed relational database service.
- A logical server acts as a management container for your databases.
- The Basic tier is perfect for learning and low traffic.
- Firewall rules are mandatory — allow only necessary IPs.
- Connection strings and secure credentials are the keys to your database.
- Scripting with Azure CLI makes creation repeatable.
Keep learning
Related tutorials, quizzes, and articles for this topic.
Discussion
Questions, corrections, and tips help everyone reading this page.
0 comments
Add a comment
No comments yet — start the thread.