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

PythonMacosPostgresqlPsycopg2

Python Problem Overview


I was trying to install postgres for a tutorial, but pip gives me error:

pip install psycopg

A snip of error I get:

Error: pg_config executable not found.

Please add the directory containing pg_config to the PATH

or specify the full executable path with the option:

    python setup.py build_ext --pg-config /path/to/pg_config build ...

or with the pg_config option in 'setup.cfg'.

Where is pg_config in my virtualenv? How to configure it? I'm using virtualenv because I do not want a system-wide installation of postgres.

Python Solutions


Solution 1 - Python

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 - Python

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 - Python

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 - Python

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

Solution 5 - Python

Here's how I was able to solve this problem on my Mac (OSX 10.9):

brew update
brew install --force ossp-uuid
brew install postgresql
pip install psycopg

I got a CLANG error when I tried pip install psycopg (an LLVM 5.1 issue), so I had to install psycopg with this command instead:

ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future pip install psycopg

It's similar to Mingyu's solution, but there are enough differences that I thought it was worth sharing.

Solution 6 - Python

you must configure path postgresql:

export PATH=$PATH:/Library/PostgreSQL/11/bin

after, you must install requirements:

pip3 install -r requirements

Solution 7 - Python

For OS X El Capitan (10.11.6) + brew + virtualenv + PostgreSQL 9.5:

After installing PostgreSQL 9.5:

brew install postgresql@9.5

Then, open your terminal and execute:

export PATH=$PATH:/usr/local/opt/postgresql\@9.5/bin/
pip install psycopg2

Solution 8 - Python

This error is caused when the build tools can't find the Postgresql libraries.

Often it's required to instruct psycopg2 how to find the pg_config binary, you can:

  1. add the path to pg_config in your shell path (/usr/local/pgsql/bin/)

  2. 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=

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

  • the above is an example, you can do locate pg_config to find out where it resides, or simply type which pg_config and it should tell you the path.

Less often the error comes from not having postgresql installed on your system. If so, download and build postgres, or download a pre-built psycopg2 binary for OS X.

Solution 9 - Python

virtualenv is for python packages. I don't think you'll be able to contain postgres inside a virtualenv. The error message you're seeing is presumably because you haven't yet installed postgres. The psycopg2 install script is looking for postgres files (in this case pg_config) and not finding them because it is not installed. postgres can't be installed using pip or virtualenv.

Solution 10 - Python

On Windows I installed postgres manually from http://www.enterprisedb.com/products-services-training/pgdownload#windows. After that the same command works.

Solution 11 - Python

In addition to the answers provided by @bkev and @andi, according to the documentation on Postgres.app, you should add the following to your bash_profile on Mac:

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

Note that, there is no hard-coded version number. I wanted to add this as a comment to above answers, but I don't have enough rep for this.

Solution 12 - Python

If you're using postgresql 9.4, the file is located in

/usr/pgsql-9.4/bin/pg_config

The name of the package is

postgresql94-9.4.9-1PGDG.rhel6.x86_64

to add pg_config to your PATH, do the following

PATH=$PATH:/usr/pgsql-9.4/bin/

Solution 13 - Python

If you don't have to use the psycopg driver specifically, switch to the pg8000 driver. It's pure Python and less finicky.

Solution 14 - Python

On Ubuntu I just needed the postgres dev package:

sudo apt-get install postgresql-server-dev-all

Solution 15 - Python

Mine was located in /Library/PostgreSQL/9.4/bin

export PATH=$PATH:/Library/PostgreSQL/9.4/bin

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKGoView Question on Stackoverflow
Solution 1 - PythonbkevView Answer on Stackoverflow
Solution 2 - PythonMingyuView Answer on Stackoverflow
Solution 3 - PythonandilabsView Answer on Stackoverflow
Solution 4 - PythonKevin DaliasView Answer on Stackoverflow
Solution 5 - PythonmrgnwView Answer on Stackoverflow
Solution 6 - PythonDiego Santa Cruz MendezúView Answer on Stackoverflow
Solution 7 - PythonvalexView Answer on Stackoverflow
Solution 8 - Pythonl'L'lView Answer on Stackoverflow
Solution 9 - PythonHoly MackerelView Answer on Stackoverflow
Solution 10 - PythonMaxView Answer on Stackoverflow
Solution 11 - PythonTurgut SarıçamView Answer on Stackoverflow
Solution 12 - PythonAhmedView Answer on Stackoverflow
Solution 13 - PythonChris JohnsonView Answer on Stackoverflow
Solution 14 - PythonmarcanuyView Answer on Stackoverflow
Solution 15 - PythonInternetFettView Answer on Stackoverflow