Skip to content

Instantly share code, notes, and snippets.

@aaugustin
Last active December 3, 2019 20:18
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save aaugustin/9135842 to your computer and use it in GitHub Desktop.
Save aaugustin/9135842 to your computer and use it in GitHub Desktop.
Connecting a Django application to a Microsoft SQL Server database from Debian GNU/Linux
  1. Install and register the FreeTDS driver for unixODBC.

     apt-get install tdsodbc
     odbcinst -i -d -f /usr/share/tdsodbc/odbcinst.ini
    
  2. (Optional) Test the DSN-less connection with pyodbc.

     apt-get install python-pyodbc
    
     >>> import pyodbc
     >>> pyodbc.connect("DRIVER=FreeTDS;SERVER=...;PORT=1433;UID=...;PWD=...;DATABASE=...")
     <pyodbc.Connection object at ...>
    
     apt-get remove python-pyodbc
    
  3. In your project's virtualenv, install django-pyodbc.

     apt-get install python-dev unixodbc-dev
     # If you have an old version of pip:
     pip install django-pyodbc
     # If you have a recent version of pip:
     pip install --allow-external pyodbc --all-unverified pyodbc django-pyodbc
    
  4. Edit the DATABASES setting in your Django project.

     DATABASES = {
         '...': {
             'ENGINE': 'django_pyodbc',
             'NAME': '...',
             'HOST': '...',
             'PORT': 1433,
             'USER': '...',
             'PASSWORD': '...',
             'OPTIONS': {
                 'host_is_server': True,
             }
         }
     }
    
  5. Test the connection in ./manage.py shell.

     >>> from django.db import connection
     >>> cursor = connection.cursor()
     >>> cursor.execute("SELECT 2 + 2")
     <pyodbc.Cursor object at ...>
     >>> cursor.fetchall()
     [(4,)]
     >>> cursor.close()
    
@wakandan
Copy link

doesn't seem to support django 1.11

@waltersdmh
Copy link

Same problem here when testing a connection. Django 1.11 is not supported

@Abhi904485
Copy link

After Lost of Searching in django docs and i think most of the link in Google regarding Django with MSSQL Server below are my Configurations.

My python version is

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)]

Required Packages are :

django==1.11.16
django-pyodbc-azure==2.1.0.0
pyodbc=4.0.24
pywin32==224

All Above packages are the latest one At the time of posting this post.

For Downloading pywin32 use this link (Latest version is 224)

https://github.com/mhammond/pywin32/releases

After that you need to download SQL SERVER driver, i am currently on windows 10 so i downloaded the latest one that 2017 SQL SERVER DRIVER.

https://www.microsoft.com/en-us/download/details.aspx?id=56567

After that in Django Settings.py file this is my Configuration

if you did not mention your driver version in option field this will not work so be attentive .

DATABASES = {
'default': {
'NAME': 'abc', #this is your database name
'ENGINE': 'sql_server.pyodbc', #this is your Engine
'HOST': 'x.x.x.x', #MSSQL SERVER ip
'USER': 'user', # username
'PASSWORD': 'password', # password
'OPTIONS': { # mention your Driver Version
'driver': 'ODBC Driver 17 for SQL Server' # Mine Driver version is 2017 so that i mentioned 17 , mention According to yourself
}
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment