Mac + virtualenv + pip + postgresql = Error: pg_config executable not found

Solution 1:

On the Mac, if you're using Postgres.app, the pg_config file is in your /Applications/Postgres.app/Contents/Versions/<current_version>/bin directory. That'll need to be added to your system path to fix this error, like this:

export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/<current_version>/bin

So for example, if the current Postgres.app version is 9.5, this export line would be:

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

With more recent versions of the Postgres.app (> 9.5?), you can simply add "latest" in place of the version number, like so:

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

Solution 2:

On Mac, the solution is to install postgresql:

brew install postgresql

On CentOS, the solution is to install postgresql-devel:

sudo yum install postgresql-devel

pg_config is in postgresql-devel package

Solution 3:

I totally agree with john hight that most of posted answers are totally offtopic assuming the OP exactly specified need of using virtualenv.

For me the answer was runing following command in prompt while having activated virtualenv:

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

(notice that part 9.4 stands for version and may vary)

or if you want to use the latest installed version of Postgres:

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

and then:

pip install psycopg2

goes sucesfully assuming you have installed postgres. And if not, then remember that the best and recomended solution is to use: Postgres.app

Solution 4:

Don't forget that your $PATH variable in the virtual environment != your global $PATH variable. You can confirm this with 'echo $PATH' in your virtualenv and also in a new shell. So, unless you want to install PostgreSQL as a unique instance inside your virtual environment (not a thing worth doing, imo), you'll need to modify the $PATH variable within the virtualenv to include the path to your global installation (which will solve your missing pg_config error).

Here are the steps:

1.) In a new shell, type 'which pg_config'. This will return the path. Copy it. In my case, the path looked like this: /Applications/Postgres.app/Contents/Versions/9.3/bin

2.) Back in your virtualenv shell, type 'export PATH=/your-path-to-pg_config:$PATH'

3.) Then, still within the virtualenv, 'pip install psycopg2'

If all goes according to plan, this will install psycopg2 within the virtual environment, but the installation will refer to your Global PostgreSQL installation. In my case, this Global installation was installed via Postgres.App, hence the path. I prefer this method of working with psycopg2 as it means I can use the database easily within any virtualenv rather than only within the defined virtual environment.

Hope this helps anyone who arrives here. For Google juice, here's the explicit (and vague) error language returned when you run into this problem:
Command python setup.py egg_info failed with error code 1