Shared State

Jeff McGee's Blog of Messages Worth Passing

Connecting to PostgreSQL in Python with PyOdbc in Mac OS X Yosemite

Posted on Sat 21 March 2015 in Tips

I spent an embarassing amount of time on this, so I thought I would document it.

brew install postgresql unixodbc psqlodbc

Start up postgres:

postgres -D /usr/local/var/postgres

Check that you can connect to the database:


Inside postgres, create a user with a password. You probably need to give them permissions and such.

create user snake with password 'like-i-would-tell-you';

Create a file ~/.odbcinst.ini. Add the following content:

[ODBC Drivers]
postodbc = Installed

Description     = PostgreSQL ODBC driver
Driver          = /usr/local/lib/
Setup           = /usr/local/lib/
Debug           = 0

Create a file ~/.odbc.ini. Add the following content:

[ODBC Data Sources]
gopostal = postodbc

Trace = 0

Driver      = /usr/local/lib/
ServerName  = localhost
Port        = 5432
Database    = postgres
Username    = snake
Password    = like-i-would-tell-you

The command isql is included with unixodbc. It is good for sanity checking your configuration:

isql -v gopostal

Once that works, you can install PyOdbc. Don't use PyPi, instead download the latest version from the website. You will need to edit PyOdbc's to make things work. Open up There is this nice big section where they add the iodbc library and supress the warnings that iodbc is deprecated. (How nice.) Change it to use odbc instead of iodbc as follows:

elif sys.platform == 'darwin':

Then, build and install PyOdbc:

pip install .

Now you should be able to connect using pyodbc:

python -c 'import pyodbc; pyodbc.connect(dsn="gopostal")'
my photo

Jeff McGee — builder, problem solver, teacher, and general nerd.

jeffamcgee AT gmail