Free tools are great, but the world ain't all sunshine and rainbows. Sometimes, we may need to connect to a Microsoft SQL Server database from one of our Python applications running under Linux. Fortunately, there are ways to achieve this.

I am assuming we got this:

  • • A Microsoft SQL Server installation running under Windows. I tested this using Microsoft SQL Server 2008 R2 Express, but hopefully this will work with other versions as well.
  • Ubuntu Linux. I am using 12.04 LTS.

Without further ado, here are the steps you should follow to get this working.

1. SQL Server setup

Your SQL Server installation must be setup to allow external connections. If the DB is not administered by you this might not be a problem, but in case you do have administrator level access and need to do it yourself, read here.

Now you must have setup a port in which SQL Server is listening. Remember it.

Make sure you are not blocked by Windows firewall or such when you attempt to connect to the Windows computer. Attempting a telnet will help us check if there are connection problems. For example, try running telnet <sqlserverpc> <port> from Ubuntu and check the connection doesn't fail.

Regarding authentication, I have only tried this with the sa login enabled (ie. not using Windows Authentication). You may read on how to do that here.

2. Install required packages under Ubuntu

These are the things we are going to need:

  • FreeTDS is is a set of libraries that allows programs to natively talk to Microsoft SQL Server databases. It's what we usually call a driver.
  • UnixODBC acts as a driver manager and is the implementation of the ODBC API.
  • pyodbc is a Python 2.x and 3.x module that allows you to use ODBC to connect to almost any database.

From a terminal, run:

sudo apt-get install unixodbc unixodbc-dev freetds-dev tdsodbc

From the Virtualenv of our Python application (if you are not using one, you should!) run pip install pyodbc.

3. Setup server in FreeTDS's settings

Edit the file /etc/freetds/freetds.conf and replace placeholders appropriately. Note that we are calling our server sqlserver.

    host = <ip address of the computer running SQL Server>
    port = <port>
    tds version = 7.0

After this you can test the connection with this command:

tsql -S sqlserver -U <username> -P <password>

Then run some SQL Server command to make sure everything works fine. For example you may run a DB query like this:

select * from <database name>.dbo.<table name>

If it worked, it will print the results of the query. Quit with Ctrl+D.

4. Setup unixODBC to use FreeTSD & add a data source

First, run odbcinst -j to know where our configuration files are located. We will need to edit two files: the "drivers" and "system data source". I assume they are /etc/odbcinst.ini and /etc/odbc.ini respectively, but the output of the command will tell you this.

Edit /etc/odbcinst.ini like this:

Description = TDS driver (Sybase/MS SQL)
# Some installations may differ in the paths
#Driver = /usr/lib/odbc/
#Setup = /usr/lib/odbc/
Driver = /usr/lib/x86_64-linux-gnu/odbc/
Setup = /usr/lib/x86_64-linux-gnu/odbc/
CPTimeout =
CPReuse =
FileUsage = 1

If the paths for Driver and Setup do not work in your installation, you can find where these files are located by running find / -name "libtds*".

Edit /etc/odbc.ini like this, to add a data source named sqlserverdatasource:

Driver = FreeTDS
Description = ODBC connection via FreeTDS
Trace = No
Servername = sqlserver
Database = <name of your database>

Now you may test the connection to out data source works by running isql -v sqlserverdatasource <username> <password>.

5. Connect to our data source from a Python application

If everything is fine, with the help of pyodbc it should be really easy! You may try the following snippet:

import pyodbc

dsn = 'sqlserverdatasource'
user = '<username>'
password = '<password>'
database = '<dbname>'

con_string = 'DSN=%s;UID=%s;PWD=%s;DATABASE=%s;' % (dsn, user, password, database)
cnxn = pyodbc.connect(con_string)

That should be it :-)


  1. Ron
    Ron on 10/17/2012 11:33 a.m.
    Excellent! You've made this real easy. I'd recommend using locate rather than find to locate the ftds libs, mine ended up in /usr/lib/odbc/ Thank you
  2. David
    David on 11/08/2012 8:11 a.m.
  3. Rima
    Rima on 12/02/2012 3:10 a.m.
    Thanks for this! Is there any idea on how to use SQL Server as a database back-end for a django application?
  4. Paul von Hoesslin
    Paul von Hoesslin on 12/06/2012 12:28 p.m.
    Alan, I've followed your tut religiously and I still obtain this error: pyodbc.Error: ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found, and no default driver specified (0) (SQLDriverConnect)') I've falled stumped, any clues?
  5. Linksular
    Linksular on 12/17/2012 5:49 a.m.
    You, sir, are a genius, Thanks! Hours have been saved here. It is interesting how you cannot insert the server details directly in the connection string using Python on Linux, and instead you have to create a DSN and reference that in the connection string... just not very user friendly imo!
  6. sapardi
    sapardi on 01/14/2013 2:50 a.m.
  7. Nathan Wienand
    Nathan Wienand on 02/08/2013 5:14 a.m.
    thank you sir ... for saving my life with this...
  8. Felipe Santiago
    Felipe Santiago on 04/09/2013 8:12 a.m.
    Thank you!!!
  9. Ajeesh T Vijayan
    Ajeesh T Vijayan on 05/02/2013 1:52 a.m.
    Excellent article.
  10. nelson
    nelson on 08/14/2013 5:46 p.m.
    Regards, this is happening to me nelson@Nelson-Latitude:/etc/freetds$ tsql -S -U sa Password: locale is "es_NI.UTF-8" locale charset is "UTF-8" using default charset "UTF-8" 63Error 20009 (severity 9): Unable to connect: Adaptive Server is unavailable or does not exist OS error 110, "Expiró el tiempo de conexión" There was a problem connecting to the server
  11. gigi
    gigi on 12/05/2013 8:22 a.m.
    Thanks a lot mate! Very useful indeed! :D
  12. Maria
    Maria on 02/26/2014 5:56 p.m.
    Thanks so much for this!
  13. Palash kulshreshtha
    Palash kulshreshtha on 04/09/2014 10:10 a.m.
    any one facing "Server name not found in configuration files." must copy his freetds.conf file to his home directory with "." for hiding it. I found out that tsql searches for connection per user here
  14. Jose
    Jose on 08/29/2014 4:44 a.m.
    Great!! thank u it works fine fron a raspberry
  15. Drew
    Drew on 09/28/2014 4:41 p.m.
    Thx for the post. Most useful!!! and worked with no changes to the code above.
  16. jqaypslg
    jqaypslg on 10/14/2014 3:30 p.m.
    Wells describesonders These are the Hill <a href="">weight loss for women</a> <a href=>venus factor</a> diet plans for women will soon bring it down.
  17. perxceus
    perxceus on 10/16/2014 3:59 p.m.
    Excuse me if English is not correct, but I'm using GoogleTranslate. Thank you for the information you have provided, it has served me. I did everything you mentioned, among other things further, however reports me the following error: locale is "es_PE.UTF-8" locale charset is "UTF-8" using default charset "UTF-8" Msg 18456 (severity 14, state 1) from PC-07 Line 1:          "Login failed for user 'username'." Error 20002 (severity 9):          Adaptive Server connection failed There was a problem connecting to the server I'm still looking on Google, but if you already know the solution to this incidence, I'd appreciate the proportions. Greetings.
  18. Grillazz
    Grillazz on 10/21/2014 6:42 a.m.
    Great post. Everything is working well!
  19. jzhanmfc
    jzhanmfc on 10/22/2014 4:53 a.m.
    <a href="">carpet cleaning Santa Monica</a> <a>Santa Monica carpet cleaning</a> upholstery cleaning Santa Monica
  20. cufiqyvf
    cufiqyvf on 10/26/2014 6:29 a.m.
    Wants the Necessities The necessities of life <a href="">green carpet cleaning</a> <a href=>cheap carpet cleaning</a> carpet cleaning in simi valley the valley, and hoping to intercept themat the stream.
  21. etgpuahn
    etgpuahn on 10/29/2014 7:16 a.m.
    There is such a storm, it looks as <a href="">szybka pożyczka przez internet</a> <a href=>pożyczka bez zaświadczeń</a> pożyczka chwilówka unusually large compared to the body, his alimentive,thoracic, muscular and bony systems are smaller and less developed thanthe average. bdyxlcwg jszebdl You must go and <a href="">szybkie pożyczki online</a> <a href=>pożyczki chwilówki online</a> szybkie pożyczki przez internet a confession to make to you. wuass alrv An earthly paradise in which men should <a href="">szybkie chwilówki</a> <a href=>pożyczki pozabankowe dla zadłużonych</a> kredyt bez biku kinsey went out. Some lady saw a <a href="">szybkie pożyczki online</a> <a href=>pożyczki chwilówki</a> pozyczki na dowod this. bbloogas esyaqd Constant repetition of the same kinds of thoughts or emotions finallymakes permanent changes <a href="">kredyty pozabankowe</a> <a href=>szybka pożyczka online</a> szybkie pożyczki online jungle made no reply. brerdthp yegrumqni So far as to <a href="">pozyczki bez biku</a> <a href=>pozyczki bez biku</a> pożyczki pozabankowe dla zadłużonych talk about the preparations for hate week. qfknzcz The agents of goldstein <a href="">pozyczki bez biku</a> <a href=>pożyczka bez zaświadczeń</a> pozyczka na dowod the cell. Machines in the <a href="">pożyczki pozabankowe dla zadłużonych</a> <a href=>pożyczki chwilówki online</a> pożyczki chwilówki that wall of virtue, even if it were. njsqmtyoo litsulyen Winston let out the water and disgustedly <a href="">pozyczka na dowod</a> <a href=>pożyczki chwilówki przez internet</a> pożyczki przez internet clung to him. txivhi

Post your comment