Using Django with SQL Server LocalDB
Updated for 2021.
How to use Django with SQL Server LocalDB
LocalDB is a version of Microsoft SQL Server Express Edition designed for developers. LocalDB is easy to install, requires no management, and is compatible with other editions of SQL Server.
LocalDB is lightweight – the setup routine is just 53MB. LocalDB instances will start automatically upon the first connection request and stop automatically shortly after the last connection is closed.
Databases hosted on LocalDB are limited to 10 GB in size.
Follow these steps to install LocalDB, create a database, and connect Django.
Note: These steps have been tested with Microsoft SQL Server 2019, Django 2.2 and 3.0. The pyodbc and django-mssql-backend packages will be used to connect Django to SQL Server.
Installing LocalDB
Step 1. Download SQL Server 2019 Express from https://go.microsoft.com/fwlink/?LinkID=866658
Step 2. Run the setup routine and click Download Media
![Using Django with SQL Server LocalDB 2 SQL Server Installing Local DB](https://eclipse-online.com/wp-content/uploads/1_sql_server_installer_download_media-1-1030x816.png)
Step 3. Choose LocalDB, specify a download location, and click Download.
![Using Django with SQL Server LocalDB 3 SQL Servier Installer](https://eclipse-online.com/wp-content/uploads/2_sql_server_installer_download_media-1-1030x816.png)
Step 4. Click Close button to close the setup routine.
![Using Django with SQL Server LocalDB 4 SQL Server Download Successful](https://eclipse-online.com/wp-content/uploads/3_sql_server_installer_download_media-1-1030x816.png)
Step 5. Run the SqlLocationDB.msi setup routine.
Step 6. Click Next button.
![Using Django with SQL Server LocalDB 5 Installation Wizard for SQL Servier LocalDB](https://eclipse-online.com/wp-content/uploads/4_LocalDB_installer-1.png)
Step 7. Accept the license agreement and click Next button.
![Using Django with SQL Server LocalDB 6 SQL Server LocalDB License Agreement](https://eclipse-online.com/wp-content/uploads/5_LocalDB_installer.png)
Step 8. Click Install button.
![Using Django with SQL Server LocalDB 7 SQL LocalDB installation](https://eclipse-online.com/wp-content/uploads/6_LocalDB_installer.png)
Step 9. Click Finish button.
![Using Django with SQL Server LocalDB 8 SQL Server Local DB Finish Installation](https://eclipse-online.com/wp-content/uploads/7_LocalDB_installer.png)
Installing related database utilities and drivers
Step 1. Download Microsoft ODBC Driver 17 for SQL Server from https://go.microsoft.com/fwlink/?linkid=2137027 and run the setup routine.
Note: This is required for the Command Line Utitlities for SQL Server.
Step 2. Click Next button on Welcome page.
![Using Django with SQL Server LocalDB 9 ODBC Driver Setup on SQL Server for Django](https://eclipse-online.com/wp-content/uploads/8_ODBC_installer.png)
Step 3. Accept the license agreement and click Next button.
![Using Django with SQL Server LocalDB 10 ODBC Driver for SQL Server License Agreement](https://eclipse-online.com/wp-content/uploads/9_ODBC_installer.png)
Step 4. Click Next button on Feature Selection page.
![Using Django with SQL Server LocalDB 11 Selecting ODBC Driver Program Features](https://eclipse-online.com/wp-content/uploads/10_ODBC_installer.png)
Step 5. Click Install button.
![Using Django with SQL Server LocalDB 12 Installiong ODBC Driver](https://eclipse-online.com/wp-content/uploads/11_ODBC_installer.png)
Step 6. Click Finish button.
![Using Django with SQL Server LocalDB 13 Complete SQL Server Installation](https://eclipse-online.com/wp-content/uploads/12_ODBC_installer.png)
Step 7. Download Microsoft Command Line Utilities 15.0 for SQL Server from https://go.microsoft.com/fwlink/?linkid=2142258 and run the setup routine.
Note: This is required to create the database and database user account from the command line.
Step 8. Click Next button on Welcome page.
![Using Django with SQL Server LocalDB 14 Download Microsoft Command Line Utilities to Support Django Database](https://eclipse-online.com/wp-content/uploads/13_command_line_utilities_installer.png)
Step 9. Accept license agreement and click Next button.
![Using Django with SQL Server LocalDB 15 Accept Microsoft Command Line Ultilities License Agreement](https://eclipse-online.com/wp-content/uploads/14_command_line_utilities_installer.png)
Step 10. Click Install button.
![Using Django with SQL Server LocalDB 16 Install Microsoft Command Line Utilities](https://eclipse-online.com/wp-content/uploads/15_command_line_utilities_installer.png)
Step 11. Click Finish button.
![Using Django with SQL Server LocalDB 17 Finish install of Microsoft Command Line Utilities to create Django Database](https://eclipse-online.com/wp-content/uploads/16_command_line_utilities_installer.png)
Creating database
LocalDB supports multiple instances. We recommend created a named instance for each Django project.
- Open command prompt as Administrator.
- Create a named instance for your project (replace “Project” with the name of your project)
sqllocaldb create ProjectLocalDB
- Start the new instance
sqllocaldb start ProjectLocalDB
- Verify the instance is running
sqllocaldb info ProjectLocalDB
- Connect to the named instance
sqlcmd -S (LocalDB)\ProjectLocalDB
- Create login for new instance
create login my_user with password='my_password' go
- Assign login to the admin role
sp_addsrvrolemember my_user, sysadmin go
- Create user
create user my_user go
- Create database
create database my_db go exit
Configure Django to connect to LocalDB database
- Install Django dependencies for connecting to SQL Server
pip install pyodbc
pip install django-pyodbc-azure
Note: django-mssql-backend supports Django version 2.2 and 3.0. If you need to work with Django 2.1 or earlier, consider using django-pyodbc-azure. - Update DATABASES settings in your Django application:
DATABASES = { 'default': { 'ENGINE': 'sql_server.pyodbc', 'HOST': '(LocalDB)\ProjectLocalDB', 'PORT': '', 'NAME': 'my_db', 'USER': 'my_user', 'PASSWORD': 'my_password', 'OPTIONS': { 'driver': 'ODBC Driver 17 for SQL Server', }, }, }
If you’re looking for business IT support in Michigan or in the cloud, reach out to us at support@eclipse-online.com or by calling 586.263.1775.