23

I'm trying to connect to a SQL Server using Pypyodbc on Mac and I'm getting the following error:

pypyodbc.DatabaseError: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'FreeTDS' : file not found")

I have installed freeTDS and unixodbc

brew install unixodbc
brew install freetds

and here is my connection String with dummy data:

connection_String = "Driver=FreeTDS;Server=123.12.12.12;tds_version=7.2;Database=db_db;Uid=username:pwd=password:port=1433"
Mo.
  • 40,243
  • 37
  • 86
  • 131
  • [Does this SO post answer you question?](http://stackoverflow.com/questions/11678696/sql-server-python-and-os-x) – AlexLordThorsen Apr 16 '15 at 07:22
  • Not a Mac guy, but in other *nix systems, I'd check that the FreeTDS so files are in LD_LIBRARY_PATH – DM Graves Apr 21 '15 at 03:31
  • The answer that I posted [on a duplicate question](https://stackoverflow.com/a/46793344/648162) may help if you have this issue. – qris Oct 18 '17 at 07:47

2 Answers2

14

Check your /etc/odbcinst.ini or it could be under /etc/unixODBC/odbcinst.ini

In your connection string you have something like this "DRIVER=FreeTDS" there needs to be an entry in that file like the following. You may need to change your driver location.

[FreeTDS]
Description             = FreeTDS unixODBC Driver
Driver          = /usr/lib64/libtdsodbc.so.0
Setup           = /usr/lib64/libtdsodbc.so.0
UsageCount              = 1
KtmDan
  • 157
  • 1
  • 3
11

First, you can use pymssql which doesn't require configuring unixODBC, which is the problem here.

To configure unixODBC with FreeTDS, do the following: http://www.unixodbc.org/doc/FreeTDS.html

There's a tool that comes with FreeTDS, tsql. Use it to test your connection string. It's a very bare bones tool and something of a pain. :-/

Finally, there's a similar question with other problems in StackOverflow: How do I configure pyodbc to correctly accept strings from SQL Server using freeTDS and unixODBC?

Community
  • 1
  • 1
Javier
  • 2,752
  • 15
  • 30
  • the link to configure unixODBC with FreeTDS is great, except there's no "libtdsodbc.so" anywhere on OS X – ierdna Dec 22 '16 at 19:46
  • In Archlinux, it's part of a package called "freetds". Maybe you can install that library. – Javier Dec 24 '16 at 03:28
  • 1
    @andrei if you already have Freetds installed, brew uninstall it first, then brew install freetds --with-unixodbc fixed my problem. See here http://stackoverflow.com/questions/28074751/freetds-missing-libtdsodbc-so-file-on-osx – alys May 17 '17 at 22:02
  • first, I got pyodbc working, then i decided to upgrade python to python3 (or rather install another version along side python2) and now i can't get it to work again. – ierdna Jul 31 '17 at 20:54
  • 2
    On MacOS Monterey, brew install freetds --with-unixodbc throws the error: invalid option: --with-unixodbc . Option is no longer required, see https://github.com/FreeTDS/freetds/issues/257 – user1255933 May 25 '22 at 00:42