DEV Community

SOURABH KUMAR VERMA
SOURABH KUMAR VERMA

Posted on

Integrating Azure SQL with Django : A Step-by-Step Guide

Hi👋 I'm Sourabh and student ambassador form Bangalore studying Computer Science from BIT


Integrating Django with Azure SQL

Migrating your Django project to a cloud-based database like Azure SQL can significantly enhance its scalability, reliability, and performance. This guide will walk you through the process of integrating Django with Azure SQL.

Watch the Demo on YouTube


Prerequisites

  1. Azure Subscription: Get Azure for Students to receive $100 in credits for 12 months.
  2. Django Project: Ensure you have an existing Django project set up.
  3. VS Code: Install Visual Studio Code for your code editing needs.

Required Packages

pip install django pyodbc mssql-django
Enter fullscreen mode Exit fullscreen mode

ODBC Driver installation
To connect Django to Azure SQL, you'll need to install the ODBC driver for SQL Server. The following command installs the ODBC Driver 17 for SQL Server depending on your operating system:

For Ubuntu/Linux:

sudo su 
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
curl https://packages.microsoft.com/config/ubuntu/20.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
exit
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install -y msodbcsql17
sudo apt-get install -y unixodbc-dev
Enter fullscreen mode Exit fullscreen mode

For macOS:

brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
brew update
brew install --no-sandbox msodbcsql17 mssql-tools
Enter fullscreen mode Exit fullscreen mode

For Windows:

You can download and install the driver from Microsoft's website.

Once the driver is installed, your Django project should be able to connect to Azure SQL using the mssql-django package.

Why Azure SQL Over SQLite3?

  • Scalability: Handles large datasets and high traffic effortlessly.
  • Reliability: Managed by Microsoft, offering high availability and disaster recovery.
  • Security: Advanced features like encryption, threat detection, and compliance.
  • Performance: Features like automatic tuning and intelligent query processing.
  • Remote Access: Access your database from anywhere.

Key benefits of Azure SQL


Step-by-Step Integration

1. Set Up Azure SQL Database

Deployment AZ SQL
📝Note - deployment can also be done through Azure Portal

  • Create a Resource Group:
  az group create -l <location> -n <MyResourceGroup>
Enter fullscreen mode Exit fullscreen mode
  • Create a SQL Server:
  az sql server create -n <server-name> -l <location> --admin-user <admin-user> --admin-password <admin-password> -g <resource-group>
Enter fullscreen mode Exit fullscreen mode
  • Create the Database:
  az sql db create -g <resource-group> -s <server-name> -n my-db --service-objective GP_Gen5_2
Enter fullscreen mode Exit fullscreen mode
  • Allow Your IP in Firewall:
  az sql server firewall-rule create --resource-group <resource-group> --server <server-name> --name AllowMyClientIP --start-ip-address <your_public_ip> --end-ip-address <your_public_ip>
Enter fullscreen mode Exit fullscreen mode

2. Install Required Packages

pip install django pyodbc mssql-django
Enter fullscreen mode Exit fullscreen mode

3. Configure Django Settings

In settings.py, update the DATABASES section:

DATABASES = {
    'default': {
        'ENGINE': 'mssql',
        'NAME': 'my-db',
        'USER': '<admin-user>',
        'PASSWORD': '<admin-password>',
        'HOST': '<server-name>.database.windows.net',
        'PORT': '',
        'OPTIONS': {
            'driver': 'ODBC Driver 17 for SQL Server',
        },
    }
}
Enter fullscreen mode Exit fullscreen mode

4. Make Migrations and Migrate

python manage.py makemigrations
python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

5. Run the Server

python manage.py runserver
Enter fullscreen mode Exit fullscreen mode

And that's it! Your Django project is now integrated with Azure SQL🎉.

Additional Resources:

Happy coding! 🚀


Useful Links

  1. Creating REST API with Python, Django, and Azure SQL
  2. Create REST API in Python with Django and Azure SQL
  3. Deploy Python (Django/Flask) App with PostgreSQL on Azure
  4. Azure SQL Migration with Data Studio
  5. Azure SQL Django Sample on GitHub

Top comments (0)