2015-11-22: Actually, looks like I was wrong. /usr/local is NOT protected by System Integrity Protection. I am leaving this post here just for reference, but your user should be able to overwrite and change files in /usr/local. To fix the original issue run: brew link --overwrite postgresql

OS X El Capitan introduced System Integrity Protection (SIP) that broke a lot of programs which required system level changes. If you do any type of postgresql development and use homebrew then likely /usr/local/lib/libpq.5.dylib is outdated. This could result in errors like the one below…

django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: dlopen(/Users/thebitguru/.virtualenvs/myenv/lib/python2.7/site-packages/psycopg2/_psycopg.so, 2): Symbol not found: _lo_lseek64
  Referenced from: /Users/thebitguru/.virtualenvs/myenv/lib/python2.7/site-packages/psycopg2/_psycopg.so
  Expected in: /usr/local/lib/libpq.5.dylib

Prior to El Capitan the solution was to change the /usr/local/lib/libpq.5.dylib to point to the updated library that you might have installed through homebrew, but with El Capitan /usr/local is a protected location so this can’t be done without disabling SIP. I am trying my best to not disable SIP for the security reasons. So, after searching for a solution I stumbled on this superuser thread. It shows the generic solution for updating library links. This seems to be a good solution to my problem.

You can look at the referenced libraries using the otool.

$ otool -L /Users/thebitguru/.virtualenvs/myenv/lib/python2.7/site-packages/psycopg2/_psycopg.so
_psycopg.so:
	/usr/local/lib/libpq.5.dylib (compatibility version 5.0.0, current version 5.7.0)
	/usr/lib/libssl.0.9.8.dylib (compatibility version 0.9.8, current version 0.9.8)
	/usr/lib/libcrypto.0.9.8.dylib (compatibility version 0.9.8, current version 0.9.8)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1225.1.1)

Note the first line about libpq.5.dylib. To fix this you can use install_name_tool to point to the correct binary. Once you have installed the updated library do the following.

install_name_tool -change /usr/local/lib/libpq.5.dylib /usr/local/Cellar/postgresql/9.4.4/lib/libpq.5.dylib /Users/thebitguru/.virtualenvs/myenv/lib/python2.7/site-packages/psycopg2/_psycopg.so

Afterwards confirm that the location is updated by confirming that the libpq.5.dylib is now pointing to the new location.

$ otool -L /Users/thebitguru/.virtualenvs/myenv/lib/python2.7/site-packages/psycopg2/_psycopg.so
_psycopg.so:
	/usr/local/Cellar/postgresql/9.4.4/lib/libpq.5.dylib (compatibility version 5.0.0, current version 5.7.0)
	/usr/lib/libssl.0.9.8.dylib (compatibility version 0.9.8, current version 0.9.8)
	/usr/lib/libcrypto.0.9.8.dylib (compatibility version 0.9.8, current version 0.9.8)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1225.1.1)

At this point, in my case, psycopg2 found the missing symbol.

Back to blog...