[Django]-Problem installing pyscopg2 on Mac OS X

11👍

This is an error that crops up when the build tools cannot find your Postgresql libraries. It means one of three things:

  1. You don’t have postgresql installed on your system. If so, download and build postgres, or download a pre-built psycopg2 binary for OS X.

  2. You have postgresql installed on your system, but you installed from a binary package and therefore don’t have the necessary libraries that psycopg2 needs. In this case, download and build postgres.

  3. More commonly, though, this means that you have built postgres on your system and just need to instruct psycopg2 how to find the pg_config binary so that it can configure the compilation. Either:

    a. put the path to pg_config in your shell path (it’s usually at /usr/local/pgsql/bin/ if you built postgres from source using the defaults.

    b. or, edit the setup.cfg file in the psycopg2 source folder and provide the full path to pg_config on the line that starts with pg_config=. Be sure to uncomment this line by removing the hash symbol at the front. If you built postgres using the defaults, the line will look something like:

    pg_config=/usr/local/pgsql/bin/pg_config

3👍

using MacPorts on Snow Leopard

pg_config=/opt/local/lib/postgresql90/bin/pg_config
👤siznax

0👍

I needed to install the psycopg2 library only in a virtualenv without adding it to the root python and I didn’t want to fully install postgreSQL on my local machine so I came up with this solution which seems to work really well.

  1. Download the postgres.app from here, and drop the app in the Applications folder.

  2. In a shell, add the postgres.app bin directory to the PATH:

export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.3/bin

  1. Activate the virtualenv:

. myvenv/bin/activate

  1. pip install psycopg2

I tried setting the pg_config variable but that didn’t work. The pip install specifically told me that I had to put the directory where pg_config was located in the path.

I also kinda prefer this solution because, if I want to run PostgreSQL locally, I can easily just start up the app and then close it when I’m done and it’s gone — nothing left running in the background. No system config changes or anything. It is a great way to run PostgreSQL for local development or a quick demo.

Leave a comment