Skip to content

Connecting to SQL Server from Python app on CentOS 7

Published: | 4 min read

Table of contents

Open Table of contents


In some projects I need to connect to SQL Server (2008 and 2012) from Python app running on CentOS 7 server. There are some steps needed to install necessary ODBC driver. Here’s how I did it.1

Install unixODBC

Microsoft’s SQL Server ODBC needs unixODBC driver to be installed, so let’s install the unixODBC driver from sources first. You can read more about the driver from unixODBC homepage.

Download and install the unixODBC driver

Download the source tarball and unpackage. I work in dir /home/user/source.

$ curl -O ''
$ tar -xz -f unixODBC-2.3.4.tar.gz

Remove any previously installed versions of the driver (optional step)

$ sudo rm /usr/lib64/libodbc*

Install the unixODBC driver.

# Change to source dir
$ cd unixODBC-2.3.4

$ ./configure --prefix=/usr --libdir=/usr/lib64 --sysconfdir=/etc --enable-gui=no --enable-drivers=no --enable-iconv --with-iconv-char-enc=UTF8 --with-iconv-ucode-enc=UTF16LE --enable-stats=no 1> configure_std.log 2> configure_err.log
$ make 1> make_std.log 2> make_err.log
$ sudo make install 1> makeinstall_std.log 2> makeinstall_err.log

# the Microsoft driver expects unixODBC to be here /usr/lib64/, so add soft links to the '.so.2' files
$ cd /usr/lib64
$ sudo ln -s
$ sudo ln -s
$ sudo ln -s

Note: Check the logs as you work on for potential issues.

Check the unixODBC installation

Check that the unixODBC installation was successful with the following commands

# Should return something like the following
$ odbc_config --version --longodbcversion --cflags --ulen --libs --odbcinstini --odbcini
-L/usr/lib64 -lodbc

$ odbcinst -j
unixODBC 2.3.4
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
USER DATA SOURCES..: /home/pyuser/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8

$ isql --version
unixODBC 2.3.4

Install the Microsoft ODBC Driver for Linux

Now we are ready to install the latter part of driver. Read more from this Microsoft’s MSDN article.

# Download the package from (Microsoft ODBC Driver 13 (Preview) for SQL Server, msodbcsql-

# Unpack
$ tar -xz -f msodbcsql-

# Change to source dir
$ cd msodbcsql-

# Verify that everything is ok for the installation
$ sudo ./ verify 1> install_std.log 2> install_err.log

(error log shows, that 2.3.1 driver is missing). Go on with the installation anyway.
$ sudo ./ install --accept-license --force 1> install_std.log 2> install_err.log

Note: Check the logs as you work on for potential issues.

Check the msodbc installation

Check the msodbc installation with the following

# Should return something like the following
$ ls -l /opt/microsoft/msodbcsql/lib64/
total 1856
-rwxr-xr-x. 1 root root 1899960 Apr 22 08:23

$ cat /etc/odbcinst.ini
should contain a section called [ODBC Driver 13 for SQL Server]

Now the driver installation part is complete.

Add SQL Server connection details

Add SQL Server connection details to ODBC driver with the following.

# Prepare a temp file for defining the DSN to your database server
$ vi /home/user/odbcadd.txt
Driver      = ODBC Driver 13 for SQL Server
Description = My MS SQL Server
Trace       = No
Server      =

# register the SQL Server database DSN information in /etc/odbc.ini
$ sudo odbcinst -i -s -f /home/pyuser/odbcadd.txt -l

# check the DSN installation with:
cat /etc/odbc.ini
should contain a section called [MyMSSQLServer]

Now we are ready to write a little test app with Python.

Test the Connection from Python

Create & activate virtualenv

$ virtualenv -p python3.5 dbdemo
$ cd dbdemo
$ . bin/activate

Install pypyodbc package with Pip

$ pip install pypyodbc

Create demo app (with an imaginary sample DB)

$ vi

The following code assumes the database to actually exist.

#!/usr/bin/env python

import pypyodbc
import datetime

connection = pypyodbc.connect('DSN=MyMSSQLServer;database=StatsDB;uid=user;pwd=passwd')

cursor = connection.cursor()

SQLCommand = ("SELECT ID, Server, EntryTime FROM Stats where ID between 1 and 10")


rowCount = 0

for row in cursor:

    # res = str(row[1])

    print ("Returned ID: " + str(row[0]) + ", Entry time:  " + str(row[2]) + ", Server: " + str(row[1]))

    rowCount = rowCount + 1


Save and exit vi.

Run the app

$ python

You should see the result from the DB. In this case you have to imagine it :)

This concludes the installation.


Few additional notes.

How to uninstall the Microsoft ODBC Driver for Linux

If the installation for some reason does not work, and you wan’t to try different version of the driver, you shoud uninstall the driver first. Read more on.

rm -f /usr/bin/sqlcmd
rm -f /usr/bin/bcp
rm -rf /opt/microsoft/msodbcsql
odbcinst -u -d -n "ODBC Driver 13 for SQL Server"



  1. Adapted from this article