130

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.

1
  • In 2024, none of the answers below helped me. I'm running on MacOS. This is what actually solved it for me: export PATH=$PATH:/Library/PostgreSQL/16/bin Commented Nov 19, 2024 at 9:44

14 Answers 14

154

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
Sign up to request clarification or add additional context in comments.

10 Comments

notice that version will change in time; now it its 9.4
be aware that to have it being applied always to current shell requires adding this line to .bash_profile if using standard Terminal's bash. IF using zsh append this line to .zshrc file. All this you will find in your home directory. (just ls -la)
This was about a million times easier than using brew.
now it is version 9.5! consider editing the answer to say current version?
you can replace <current_version> with latest which points to latest installed Postgres version. Mine folder looks like: ls /Applications/Postgres.app/Contents/Versions/ and it gives 9.5/ latest/
|
145

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

3 Comments

The question prefaces with a "virtualenv" setup. I don't believe brew is appropriate in this case. Seems like it should be a pure pip solution.
psycopg is a binding to the C library, so you will have to have the C library installed. It is not possible (AFAIK) to install native C libraries into virtualenvs.
Once you have installed the postgresql C libraries, you can then rerun the failing pip install command (this was not made clear by the above answer).
44

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

3 Comments

This is the best answer for this question.
export PATH="/Applications/Postgres.app/Contents/Versions/latest/bin:$PATH" if you want to use the latest installed version of Postgres
For all the fuss about not using the virtualenv, you realise this postgres installation is system wide, just not on the path.. and that this is the same, if not WORSE than brew install?
19

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

2 Comments

Still, I think the question implies a "unique instance inside" virtualenv. I'm still hunting for a solution that gets pg installed within the virtualenv.
You are the real champ and saved my day.
11

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.

2 Comments

+1 Worked for me. Can't believe I have to specify a custom flag to a C compiler to set up a postgres+python development environment on MacOS...
The question prefaces with a "virtualenv" setup. I don't believe brew is appropriate in this case. Seems like it should be a pure pip solution.
10

you must configure path postgresql:

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

after, you must install requirements:

pip3 install -r requirements

Comments

8

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

After installing PostgreSQL 9.5:

brew install [email protected]

Then, open your terminal and execute:

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

Comments

6

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.

1 Comment

Sure ... but just how do you do this within a virtualenv, and not be dependendent on a system-wide installation of pg?
3

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.

3 Comments

it's possible to install postgresql in a virtualenv and often this error is a path problem, regardless of postgresql being installed.
@HolyMackerel, thanks for the most relevant answer. Any details to offer on why you can't install psycopg2/pg within virtualenv?
postgres has nothing to do with python. They run separately but talk to each other.
1

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/

Comments

1

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

Comments

0

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.

Comments

0

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

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

Comments

0

please don't use brew to install postgresql, this is a local problem and not something that should be installed systemwide.

Check out requirements.txt file and see if your requirement is psycopg2 or psycopg2-binary.

If you have psycopg2, you should run the following.

pip install psycopg2-binary

pip freeze > requirements.txt

pip install -r requirements.txt

This should replace the psycopg2 with the binary (You need the binary, it's like an executable on windows and that needs to be used instead of the psycopg2).

I hope this works!

1 Comment

Be aware that there are situations in which psycopg2-binary may not work - see psycopg.org/docs/install.html#psycopg-vs-psycopg-binary.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.