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

14

127

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.

Knot answered 24/11, 2013 at 3:55 Comment(0)
T
151

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
Thrive answered 14/6, 2014 at 6:38 Comment(10)
notice that version will change in time; now it its 9.4Pentha
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)Pentha
This was about a million times easier than using brew.Derisive
now it is version 9.5! consider editing the answer to say current version?Dunsinane
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/Univocal
this worked perfectly for me and just used /latest/bin for the version, thanksFoilsman
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.Perfecto
Worked for postgres 9.6. This should be marked as an answer.Gibb
Answer worked great for any Postgresql version lower than 10. I'm guessing psycopg2 had a problem parsing a different versioning structureMarmara
/latest/ doesn't seem to work, I had to change it to /12/, and then which pg_config worked.Petard
I
139

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

Isallobar answered 14/3, 2014 at 5:18 Comment(3)
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.Independency
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.Nudge
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).Nudge
P
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

Pentha answered 2/11, 2014 at 0:31 Comment(3)
This is the best answer for this question.Jetliner
export PATH="/Applications/Postgres.app/Contents/Versions/latest/bin:$PATH" if you want to use the latest installed version of PostgresEckstein
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?Stripy
S
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

Shakespeare answered 15/3, 2014 at 21:6 Comment(2)
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.Independency
You are the real champ and saved my day.Popple
C
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.

Chitterlings answered 16/4, 2014 at 1:4 Comment(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...Plight
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.Independency
S
10

you must configure path postgresql:

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

after, you must install requirements:

pip3 install -r requirements
Sleepyhead answered 12/11, 2019 at 4:39 Comment(0)
D
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
Daisie answered 20/4, 2017 at 13:21 Comment(0)
H
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.

Henpeck answered 24/11, 2013 at 4:34 Comment(1)
Sure ... but just how do you do this within a virtualenv, and not be dependendent on a system-wide installation of pg?Independency
K
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.

Kellerman answered 24/11, 2013 at 4:9 Comment(3)
it's possible to install postgresql in a virtualenv and often this error is a path problem, regardless of postgresql being installed.Frenzy
@HolyMackerel, thanks for the most relevant answer. Any details to offer on why you can't install psycopg2/pg within virtualenv?Independency
postgres has nothing to do with python. They run separately but talk to each other.Pompom
B
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/
Billiot answered 13/9, 2016 at 3:46 Comment(0)
E
1

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

Example answered 8/2, 2018 at 0:38 Comment(0)
Z
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.

Ziegfeld answered 10/3, 2016 at 17:14 Comment(0)
S
0

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

export PATH=$PATH:/Library/PostgreSQL/9.4/bin
Semifluid answered 8/2, 2018 at 0:14 Comment(0)
P
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!

Pajamas answered 27/12, 2023 at 22:21 Comment(1)
Be aware that there are situations in which psycopg2-binary may not work - see psycopg.org/docs/install.html#psycopg-vs-psycopg-binary.Lunik

© 2022 - 2024 — McMap. All rights reserved.