124

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.

KGo
  • 18,536
  • 11
  • 31
  • 47

13 Answers13

147

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
bkev
  • 1,835
  • 2
  • 16
  • 12
  • 5
    notice that version will change in time; now it its `9.4` – andilabs Feb 02 '15 at 02:26
  • 2
    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`) – andilabs Feb 03 '15 at 22:52
  • 2
    This was about a million times easier than using brew. – Nate Mar 27 '15 at 20:57
  • now it is version 9.5! consider editing the answer to say current version? – AZhao Feb 06 '16 at 02:47
  • 6
    you can replace `` 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/` – Raymond Apr 19 '16 at 03:57
  • this worked perfectly for me and just used /latest/bin for the version, thanks – easy_c0mpany80 Sep 17 '16 at 07:40
  • Oh my god, thank you so much! Such an elegant solution :) And with the trick of latest it works like a charm also for future releases. – pasql Jan 13 '17 at 12:55
  • Worked for postgres 9.6. This should be marked as an answer. – bak2trak Jun 12 '19 at 06:43
  • Answer worked great for any Postgresql version lower than 10. I'm guessing psycopg2 had a problem parsing a different versioning structure – RoyalBigMack Jan 09 '20 at 10:20
  • `/latest/` doesn't seem to work, I had to change it to `/12/`, and then `which pg_config` worked. – Safwan Samsudeen Nov 14 '20 at 05:40
135

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

Mingyu
  • 31,751
  • 14
  • 55
  • 60
  • 13
    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. – john hight Sep 17 '14 at 23:33
  • 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. – Penguin Brian Feb 23 '17 at 04:27
  • 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). – Penguin Brian Feb 23 '17 at 04:30
43

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

andilabs
  • 22,159
  • 14
  • 114
  • 151
  • 5
    This is the best answer for this question. – Vinod Sharma Sep 28 '15 at 01:34
  • 5
    `export PATH="/Applications/Postgres.app/Contents/Versions/latest/bin:$PATH"` if you want to use the latest installed version of Postgres – jaynp Sep 08 '18 at 23:16
  • 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? – Kevin Glasson Sep 12 '22 at 06:14
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

Kevin Dalias
  • 336
  • 2
  • 5
10

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.

Community
  • 1
  • 1
mrgnw
  • 1,771
  • 2
  • 17
  • 19
  • 2
    +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... – Andomar Apr 23 '14 at 07:18
  • 1
    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. – john hight Sep 17 '14 at 23:34
10

you must configure path postgresql:

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

after, you must install requirements:

pip3 install -r requirements
8

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
Community
  • 1
  • 1
valex
  • 5,163
  • 2
  • 33
  • 40
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.

l'L'l
  • 44,951
  • 10
  • 95
  • 146
  • 1
    Sure ... but just how do you do this within a virtualenv, and not be dependendent on a system-wide installation of pg? – john hight Sep 17 '14 at 23:38
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.

Holy Mackerel
  • 3,259
  • 1
  • 25
  • 41
  • 1
    it's possible to install postgresql in a virtualenv and often this error is a path problem, regardless of postgresql being installed. – l'L'l Nov 24 '13 at 04:37
  • 1
    @HolyMackerel, thanks for the most relevant answer. Any details to offer on why you can't install psycopg2/pg within virtualenv? – john hight Sep 17 '14 at 23:41
  • 1
    postgres has nothing to do with python. They run separately but talk to each other. – Bioto Feb 03 '15 at 22:33
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/
Ahmed
  • 2,825
  • 1
  • 25
  • 39
1

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

Chris Johnson
  • 20,650
  • 6
  • 81
  • 80
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.

0

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

export PATH=$PATH:/Library/PostgreSQL/9.4/bin
InternetFett
  • 11
  • 1
  • 2