1. psycopg2 OS X – _PQbackendPID

    If you are having problems with _PQbackendPID, psycopg2 and OS X and have tried the forcing to 32 bit mentioned in other posts about the web then it might be time to try a more forceful approach. This is an example of the error that you might face:

    [cc lang="bash"]
    dlopen(/Library/Python/2.6/site-packages/psycopg2/_psycopg.so, 2): Symbol not found: _PQbackendPID
    Referenced from: /Library/Python/2.6/site-packages/psycopg2/_psycopg.so
    Expected in: flat namespace
    in /Library/Python/2.6/site-packages/psycopg2/_psycopg.so
    [/cc]

    The first thing to do is get rid of your old Psycopg2 (just in case), so locate the site-packages directory:

    [cc lang="bash"]python -c “from distutils.sysconfig import get_python_lib; print get_python_lib()”[/cc]

    Then in that directory:
    [cc lang="bash"]
    rm -rf psycopg*
    [/cc]

    Then remove the old version of PostGreSQL as much as possible. Not sure what version you might have installed, but if you have pg_config on your sys path it will tell you where it is. These instructions may or may not work for you:

    1. Stop the server. sudo launchctl unload /Library/LaunchDaemons/com.edb.launchd.postgresql-8.4.plist
    2. Remove the plist file
      sudo rm -f /Library/LaunchDaemons/com.edb.launchd.postgresql-8.4.plist
    3. Remove the Applications Menu.
      sudo rm -f “/Applications/PostgreSQL 8.4″
    4. Remove the installation directory
      sudo rm -rf /Library/PostgreSQL/8.4
      (Note: You can backup your data directory in case you need it.)
    5. Remove the ‘postgres’ user
      sudo dscl . delete /Users/postgres
      (Note: This step is optional)
    6. Remove the ini file
      sudo rm -f /etc/postgres-reg.ini

    I found a reboot worked to make sure that we are back to normal and nothing is installed.

    Download psycopg2-2.2.1.tar.gz
    Download PostGreSQL from http://www.enterprisedb.com/products/download.do – the version that isn’t the “Standard Server” but just “PostgreSQL 8.4 ” – you do have to register for that.

    Install Postgresql – making sure that it completes, if not it means that you probably had some old files or users in there. If it went ok it should have installed and started running on port 5432. Try removing it /Library/PostgreSQL/8.4/uninstall-postgresql.app – and then removing the user or files that it complained about.

    Download and install http://www.python.org/download/releases/2.6.5/ Mac Installer disk image. Restart (not sure that this restart is totally needed, but I did it anyway).

    Now when you are on a new terminal window and type “python” it should say version 2.6.5. You will have to reinstall any python bits and pieces that you have installed previously – but make sure that you reinstall PIP and easy_install – they will use the old version of Python if you are not careful.

    Now go into the directory of psycopg2-2.2.1 that you downloaded earlier.

    Change setup.cfg so it looks like the following:

    [cc lang="bash"]
    [build_ext]
    define=PSYCOPG_EXTENSIONS,PSYCOPG_NEW_BOOLEAN,HAVE_PQFREEMEM,HAVE_PQPROTOCOL3

    # PSYCOPG_EXTENSIONS enables extensions to PEP-249 (you really want this)
    # PSYCOPG_DISPLAY_SIZE enable display size calculation (a little slower)
    # HAVE_PQFREEMEM should be defined on PostgreSQL >= 7.4
    # HAVE_PQPROTOCOL3 should be defined on PostgreSQL >= 7.4
    # PSYCOPG_DEBUG can be added to enable verbose debug information
    # PSYCOPG_OWN_QUOTING can be added, but it is deprecated (will go away in 2.1)
    # PSYCOPG_NEW_BOOLEAN to format booleans as true/false vs ‘t’/'f’

    # Set to 1 to use Python datatime objects for default date/time representation.
    use_pydatetime=1

    # If the build system does not find the mx.DateTime headers, try
    # uncommenting the following line and setting its value to the right path.
    #mx_include_dir=

    # For Windows only:
    # Set to 1 if the PostgreSQL library was built with OpenSSL.
    # Required to link in OpenSSL libraries and dependencies.
    have_ssl=0

    # Statically link against the postgresql client library.
    static_libpq=0

    # “pg_config” is the preferred method to locate PostgreSQL headers and
    # libraries needed to build psycopg2. If pg_config is not in the path or
    # is installed under a different name uncomment the following option and
    # set it to the pg_config full path.
    pg_config=/Library/PostgreSQL/8.4/bin/pg_config

    # If “pg_config” is not available, “include_dirs” can be used to locate
    # postgresql headers and libraries. Some extra checks on sys.platform will
    # still be done in setup.py.
    # The next line is the default as used on psycopg author Debian laptop:
    #include_dirs=/usr/local/lib

    # Uncomment next line on Mandrake 10.x (and comment previous ones):
    #include_dirs=/usr/include/pgsql/8.0:/usr/include/pgsql/8.0/server

    # Uncomment next line on SUSE 9.3 (and comment previous ones):
    #include_dirs=/usr/include/pgsql:/usr/include/pgsql/server

    # If postgresql is installed somewhere weird (i.e., not in your runtime library
    # path like /usr/lib), just add the right path in “library_dirs” and any extra
    # libraries required to link in “libraries”.
    library_dirs=/Library/PostgreSQL/8.4/lib
    libraries=/usr/lib
    [/cc]

    Now run the installation using the new python:

    [cc lang="bash"]python setup.py install[/cc]

    This should install. You should be able to do “import psycopg2″ from the python prompt. Now install all the libraries and python modules that you need, and create the Postgresql database that you want (pgadmin3 is the best tool for this).

    Please note this was done on a OS X Snow Leopard 10.6.4 with XCode and the 10.4 libraries installed.