Skip to content

Instantly share code, notes, and snippets.

@arvsrao
Last active July 28, 2023 04:54
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save arvsrao/5432047 to your computer and use it in GitHub Desktop.
Save arvsrao/5432047 to your computer and use it in GitHub Desktop.
Guide to accessing MS SQL Server in Mac OS X via PyODBC

Since I spent essentially two full days figuring out how to access a corporate MS SQL database pythonicly, I figured I should leave some notes, for future reference and to aid other souls looking to do the same.

These instructions and the commands that follow, were executed on a MAC OS 10.8.3 system. Additionally, I found this blog post especially helpful during the debugging process.

On mac os, there is a default ODBC manager, iODBC. Other Unix based systems tend to use unixODBC. Look elsewhere for a discussion about the differences between these driver managers. The only feature we care about is being able to connect to SQL databases through pyodbc, and at the time of this writing pyodbc requires iODBC as its manager.

Start by installing freeTDS libraries. FreeTDS allows unix programs to talk natively with MS SQL and SyBase databases.

brew intsall freetds

FreeTDS needs to be configured, so edit your freetds.conf in /usr/local/etc/ or ~/.freetds.conf

nano ~/.freetds.conf

Under # A typical Microsoft server you'll see host, port, and tds version variables. These and these only should be set.

host = myserver.company.com
port = 1433
tds version = 7.2 (for MS SQL 2008)

Create ~/Library/ODBC/odbc.ini and fill out the file like so:

[MyDB]
Description = Company MS SQL
TDS_Version = 7.2
Driver = /usr/local/lib/libtdsodbc.so
Server = *********
Port = 1433

The driver is setup, so now install pyodbc:

sudo pip install pyodbc

Now try to hit your DB:

import pyodbc as p
conn = p.connect("DSN=mysql01;UID=username;PWD=password")

Should resolve without a problem. See pyodbc docs for help with executing queries, etc.

Copy link

ghost commented Sep 23, 2013

I am in the same boat - this is driving me up a wall. I worked through this but still no joy. I am at a loss.

@bgribble
Copy link

bgribble commented Oct 8, 2013

I got this toolchain working after a bit of banging on it. The key is to figure out where your ODBC libs are finding their odbc.ini files.

All the instrux I found said to install them using odbcinst, but that wasn't working for me. I finally found a odbc.ini in /Library/ that was taking precedence over everything else. Editing that one worked.

I also didn't have much luck with getting a driver defininition in odbcinst.ini to be found -- I was forever getting dlopen errors while trying to find the library file "FreeTDS" :) So I used the full path to the freetds library (the file is named "libtdsodbc.so", find it where it's installed on your system, probably under /usr/local somewhere) in the odbc.ini under /Library/ and that was the final piece. All working now, except for some mysterious exceptions on some queries that I don't get under Linux.

@wilfrido12
Copy link

@bgribble This helped me a lot, thank you

@ruimartinsptl
Copy link

ruimartinsptl commented Oct 20, 2018

Thanks,
(You have an little error: brew intsall freetds -> brew install freetds.)

I also used this solution that works: https://stackoverflow.com/questions/44527452/cant-open-lib-odbc-driver-13-for-sql-server-sym-linking-issue

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