I want to connect my django application to MS-SQL server 2014 database. I wrote this code for making connections.
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'HOST':'DESKTOP-6UNRAN0',
'PORT':'1433',
'NAME': 'MOVIE',
'COLLATION' : '',
}
}
I have installed sql_server.pyodbc
pip install django-pyodbc-azure
as mentioned in the documentation https://pypi.org/project/django-pyodbc-azure/. I am still getting error
django.db.utils.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
I no longer recommend using
django-pyodbc-azure
, as it is no longer maintained by the author. The active PyPI project for SQL Server in Django is currentlydjango-mssql-backend
. However, it only supports Django 2.2 and above. I would highly recommend upgrading to Django 2.2 (a long term support release), if not Django 3.0. 2.1 is no longer supported, and this will save you headaches down the road for a little bit of work now. I'm going to assume you're on Linux.Step One: Install Microsoft's Driver for Linux (You May Also Use FreeTDS)
If you want to use Microsoft's driver, you can install it like this:
Step Two: Create a Database and Service User in SQL Server
In SQL Server, set up a service user to your Django database. This script will create a user with the minimum permissions needed to the underlying database.
Step Three: Configure Django
Finally, let's set up Django itself to point to SQL Server. In your Django project with your
venv
activated:pip install django-mssql-backend
If you're using FreeTDS or another driver, change the
OPTIONS
line,'driver': 'ODBC Driver 17 for SQL Server'
. That should do it.Good luck!