To use MS SQL Server as the database for your Django application, follow these steps:
1. Install Required Packages
Django does not support SQL Server out of the box, so you need a third-party database backend:
After setting up and activating virtual environment, install Django, then install mssql-django
pip install mssql-django
2. Update DATABASES in settings.py
Open your Django project’s settings.py and configure the DATABASES setting:
DATABASES = {
'default': {
'ENGINE': 'mssql',
'NAME': 'your_db_name',
'USER': 'your_db_user',
'PASSWORD': 'your_db_password',
'HOST': 'your_db_host', # For local: 'localhost' or '127.0.0.1'
'PORT': '1433', # Default SQL Server port
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server', # Or 'ODBC Driver 18 for SQL Server' if installed
},
}
}
3. Install ODBC Driver
Depending on your operating system, install the appropriate ODBC driver:
- Windows:
- 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
4. Run Migrations
After updating your database settings:
python manage.py makemigrations
python manage.py migrate