2

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)')

FlipperPA
  • 13,607
  • 4
  • 39
  • 71
shaswat kumar
  • 369
  • 8
  • 19
  • do you have odbc driver installed – iklinac May 16 '20 at 17:37
  • The linked answer is completely wrong for the subject asked here. This is asking how to connect to SQL Server, a database, not how to run IIS on Windows connecting to PostgreSQL. This should be reopened. – FlipperPA May 17 '20 at 02:47
  • I have installed odbc driver @iklinac. I dont understand why this question is duplicate, the link shown definetly not answers my question. – shaswat kumar May 17 '20 at 06:14
  • what version of driver you have installed and what platform do you have, x64 or x86 for python and driver – iklinac May 17 '20 at 15:12
  • @iklinac version = 2.1.0.0 (pyodbc-azure) , my django version = 2.1.15, my python version=3.6.4 – shaswat kumar May 18 '20 at 04:07
  • @shaswatkumar asked you about ODBC driver version that you have installed and not python package version, also you haven't answered regarding which arhitecture x86 or x64 you have installed driver and python for – iklinac May 18 '20 at 04:26
  • according to this django documentation (https://docs.djangoproject.com/en/3.0/ref/databases/) for connecting to MS-SQL i have downloaded (pyodbc-azure 2.1.0.0), architecture = x64, no other specific ODBC driver mentioned in documentation – shaswat kumar May 18 '20 at 04:37

2 Answers2

8

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 currently django-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:

sudo curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/mssql-release.repo

sudo yum remove unixODBC-utf16 unixODBC-utf16-devel
sudo ACCEPT_EULA=Y yum install msodbcsql17

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.

/*
This Script Creates a SQL Server Database, Login and User
With Appropriate Permissions for a Production Django Project
with migrations. Simply fill out the variables below (@db_name and @db_password)
Username will be set to database name + '_user' by default.
*/
DECLARE @db_name VARCHAR(MAX) = 'project'
DECLARE @db_password VARCHAR(MAX) = 'project_password'
DECLARE @db_user VARCHAR(MAX) = @db_name + '_user'
--
--
USE master
DECLARE @cmd VARCHAR(MAX)
-- Server scope: create SQL Server login and permissions
SET @cmd = 'CREATE LOGIN ' + @db_user + ' WITH PASSWORD = ''' + @db_password + ''''
EXEC(@cmd)
SET @cmd = 'GRANT VIEW SERVER STATE TO ' + @db_user
EXEC(@cmd)
SET @cmd = 'CREATE DATABASE [' + @db_name + ']'
EXEC(@cmd)
-- DB scope: create user for server login and permissions
SET @cmd = 'USE [' + @db_name + '];'
SET @cmd = @cmd + 'CREATE USER ' + @db_user + ' FOR LOGIN ' + @db_user + ';'
SET @cmd = @cmd + 'GRANT SELECT, INSERT, UPDATE, DELETE, ALTER, CREATE TABLE, REFERENCES, EXEC TO ' + @db_user
EXEC(@cmd)

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

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'HOST': 'dbserver.your-domain.com',
        'PORT': '1433',
        'NAME': 'project',
        'USER': 'project_user',
        'PASSWORD': 'project_password',
        'OPTIONS': {
            'driver': 'ODBC Driver 17 for SQL Server',
            'unicode_results': True,
        },
    },
}

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!

FlipperPA
  • 13,607
  • 4
  • 39
  • 71
  • thank you for sharing your solution. Have you ever encountered this error: `nothing provides e2fsprogs needed by msodbcsql17-17.7.2.1-1.x86_64` when trying to install msodbcsql17 `yum install msodbcsql17`? Not sure if it matters, but I'm running this inside a container with base image `registry.access.redhat.com/ubi8/python-38`. Thank you for your time – Zhao Li May 11 '21 at 02:00
0

It is expected that you know if you want to connect to SQL Server you'll have to use/install ODBC as it is native data access API

Regarding documentation lets look into following lines

a Django Microsoft SQL Server external DB backend that uses ODBC by employing the pyodbc library


Compatible with Micosoft ODBC Driver for SQL Server, SQL Server Native Client, and FreeTDS ODBC drivers


OPTIONS

Dictionary. Current available keys are:

driver

String.

Server Native Client 11.0", "FreeTDS" etc). Default is "ODBC Driver 13 for SQL Server".

iklinac
  • 14,944
  • 4
  • 28
  • 30
  • I have downloaded and installed ODBC now. The problem is when I specify 'driver'='ODBC Driver 13 for SQL Server' in my database connnection code. I am able to create my migration files, but when i try to migrate (error:settings.DATABASES is improperly configured. Please supply the ENGINE value.). But when I supply engine value ('ENGINE': 'sql_server.pyodbc',), I am not able to create my migration files. i.e. migration files are not created if i give both engine and server value in my database code. – shaswat kumar May 18 '20 at 05:52