ChatGPT解决这个技术问题 Extra ChatGPT

pg_config executable not found

I am having trouble installing psycopg2. I get the following error when I try to pip install psycopg2:

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'.

----------------------------------------
Command python setup.py egg_info failed with error code 1 in /tmp/pip-build/psycopg2

But the problem is pg_config is actually in my PATH; it runs without any problem:

$ which pg_config
/usr/pgsql-9.1/bin/pg_config

I tried adding the pg_config path to the setup.cfg file and building it using the source files I downloaded from their website (http://initd.org/psycopg/) and I get the following error message!

Error: Unable to find 'pg_config' file in '/usr/pgsql-9.1/bin/'

But it is actually THERE!!!

I am baffled by these errors. Can anyone help please?

By the way, I sudo all the commands. Also I am on RHEL 5.5.

When you run commands as sudo, $PATH is changed. Can you please double check your $PATH as root?
In my case, i run ln -s /usr/pgsql-9.1/bin/pg_config /usr/sbin/pg_config and every thing ok!

d
deepwell

pg_config is in postgresql-devel (libpq-dev in Debian/Ubuntu, libpq-devel on Centos/Fedora/Cygwin/Babun.)


Solved after installing libpq-dev on ubuntu lucid(10.04). Thank You in Advance.
pg_config of postgress.app is at /Applications/Postgres.app/Contents/Versions/<your version>/bin (Mac OS X)
It's libpqxx-devel on fedora.
sudo apt-get install libpq-dev worked for me on Ubuntu
on Alpine Linux: apk add postgresql-dev
s
sacuL

On Mac OS X, I solved it using the homebrew package manager

brew install postgresql

Excellent! This solved the problem for me, but newbies should be aware that you need to install the Homebrew package manager first in order for the brew command to work.
Worked for me also, even though I did the brew install after the pgadmin install.
Doesn't that install the entire database?
I need the extra brew link postgresql
To add to @seane's comment, use this walkthrough to install homebrew.
p
phoenix

Have you installed python-dev? If you already have, try also installing libpq-dev

sudo apt-get install libpq-dev python-dev

From the article: How to install psycopg2 under virtualenv


In my computer,I only install the client of Postgresql,So that you must install the libpq-dev and the python-dev.
Doubtful that python-dev is needed.
python3-dev is needed. Without it I get Python.h: No such file or directory
one more dependency required for this is apt-get install -y gcc
After upgrading to python 3.9 I have to execute: sudo apt install python3.9-dev
B
Boris Verkhovskiy

Also on OSX. Installed Postgress.app from http://postgresapp.com/ but had the same issue.

I found pg_config in that app's contents and added the dir to $PATH.

It was at /Applications/Postgres.app/Contents/Versions/latest/bin. So this worked: export PATH="/Applications/Postgres.app/Contents/Versions/latest/bin:$PATH".


@GianlucaLodigiani you can also do this if you dont want to restart the console PATH="/Applications/Postgres.app/Contents/Versions/latest/bin:$PATH" pip install psycopg2
Perfect BigSur Apple Silicon Works like a charm. I did add the same to ~/.zshrc so that its always available for me.
worked on Mac Monterey.
j
jakub

You can install pre-compiled binaries on any platform with pip or conda:

python -m pip install psycopg2-binary

or

conda install psycopg2

Please be advised that the psycopg2-binary pypi page recommends building from source in production:

The binary package is a practical choice for development and testing but in production it is advised to use the package built from sources

To use the package built from sources, use python -m pip install psycopg2. That process will require several dependencies (documentation) (emphasis mine):

A C compiler. The Python header files. They are usually installed in a package such as python-dev. A message such as error: Python.h: No such file or directory is an indication that the Python headers are missing. The libpq header files. They are usually installed in a package such as libpq-dev. If you get an error: libpq-fe.h: No such file or directory you are missing them. The pg_config program: it is usually installed by the libpq-dev package but sometimes it is not in a PATH directory. Having it in the PATH greatly streamlines the installation, so try running pg_config --version: if it returns an error or an unexpected version number then locate the directory containing the pg_config shipped with the right libpq version (usually /usr/lib/postgresql/X.Y/bin/) and add it to the PATH: $ export PATH=/usr/lib/postgresql/X.Y/bin/:$PATH You only need pg_config to compile psycopg2, not for its regular usage.

Once everything is in place it’s just a matter of running the standard: $ pip install psycopg2 or, from the directory containing the source code: $ python setup.py build $ python setup.py install


On Ubuntu, I needed to install the libpq-dev library first and then just run the pip install command again: apt install libpq-dev
That solved it on a Mac OS X without needing to install postgresql altogether
Thank you for pointing out the documentation's recommendation to build from source. I second this suggestion--devs should use the later half of this answer and avoid the pre-packaged binary, even for most development purposes.
r
rkoval

On alpine, the library containing pg_config is postgresql-dev. To install, run:

apk add postgresql-dev

r
radtek

This is what worked for me on CentOS, first install:

sudo yum install postgresql postgresql-devel python-devel

On Ubuntu just use the equivilent apt-get packages.

sudo apt-get install postgresql postgresql-dev python-dev

And now include the path to your postgresql binary dir with you pip install, this should work for either Debain or RHEL based Linux:

sudo PATH=$PATH:/usr/pgsql-9.3/bin/ pip install psycopg2

Make sure to include the correct path. Thats all :)


On Ubuntu it's sudo apt-get install postgresql postgresql-dev python-dev, and this worked for me.
also works on Amazon Linux AMI as of Dec 2016. Also make sure gcc is installed and python3-devel.
On Amazon Linux the first command worked nice.
Y
Younes Belouche

For ubuntu users, this is the solution:

sudo apt install libpq-dev

It worked for me.


And on Arch/Manjaro it is postgresql-libs
it wokred for me too. whats the problem here? what is libpq-dev used for?
m
matiu
apt-get build-dep python-psycopg2

A
Ali Raza Bhayani

Just to sum up, I also faced exactly same problem. After reading a lot of stackoverflow posts and online blogs, the final solution which worked for me is this:

1) PostgreSQL(development or any stable version) should be installed before installing psycopg2.

2) The pg_config file (this file normally resides in the bin folder of the PostgreSQL installation folder) PATH had to be explicitly setup before installing psycopg2. In my case, the installation PATH for PostgreSQL is:

/opt/local/lib/postgresql91/

so in order to explicitly set the PATH of pg_config file, I entered following command in my terminal:

PATH=$PATH:/opt/local/lib/postgresql91/bin/

This command ensures that when you try to pip install psycopg2, it would find the PATH to pg_config automatically this time.

I have also posted a full error with trace and its solution on my blog which you may want to refer. Its for Mac OS X but the pg_config PATH problem is generic and applicable to Linux also.


for me on OS X the path looked like this PATH="/Applications/Postgres.app/Contents/Versions/9.3/bin:$PATH"
@andi Yea, the installation path of Postgres may differ but the installation recipe for psycopg2 which I mentioned above remains the same. Hope it helped.
L
Lucas Mendes Mota Da Fonseca

You should add python requirements used in Postgres on Ubuntu. Run:

sudo apt-get install libpq-dev python-dev

M
Mahrez BenHamad

You have to install libpq-dev/postgresql-libs, which is the header files and static library for compiling C programs to link with the libpq library in order to communicate with a PostgreSQL database backend.

On Arch this will run:

$ sudo pacman -S postgresql-libs

On Debian and Ubuntu:

$ sudo apt-get install libpq-dev

On Mac OS X:

$ brew install postgresql

On Red Hat/CentOS/Fedora:

$ sudo yum install postgresql-devel

Upvoted for helpfully explaining the precise cause of the issue.
n
novasaint

sudo apt-get install libpq-dev works for me on Ubuntu 15.4


If you use newer Ubuntu, still use aforementioned installation instruction. See pg_config
m
mthecreator

On Linux Mint sudo apt-get install libpq-dev worked for me.


t
thefourtheye

UPDATE /etc/yum.repos.d/CentOS-Base.repo, [base] and [updates] sections ADD exclude=postgresql*

curl -O http://yum.postgresql.org/9.1/redhat/rhel-6-i386/pgdg-centos91-9.1-4.noarch.rpmr  
rpm -ivh pgdg-centos91-9.1-4.noarch.rpm

yum install postgresql  
yum install postgresql-devel

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

pip install psycopg2

M
Milo

For those running OS X, this solution worked for me:

1) Install Postgres.app:

http://www.postgresql.org/download/macosx/

2) Then open the Terminal and run this command, replacing where it says {{version}} with the Postgres version number:

export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/{{version}}/bin

e.g.

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


Or symlink it to the latest version sudo ln -s /Applications/Postgres.app/Contents/Versions/latest/bin/pg_config /usr/local/bin/pg_config
On my Mac, I exported the PATH from ~/.zprofile file.
R
Rishabh Agarwal

I had this issue because I didn't had a postgres install. If you have brew install run

brew install postgresql

This should fix the issue.


A
Aric Kuter

Simply run the following:

sudo apt install libpq-dev

Fixed the issue for me


M
Marboni

Try to add it to PATH:

PATH=$PATH:/usr/pgsql-9.1/bin/ ./pip install psycopg2

-bash: cd: /usr/pgsql-x.x/bin/: No such file or directory
@pylover, you need to use correct path to you Postgres bin directory. Are you sure that /usr/pgsql-x.x/bin/ is correct?
It would also be sufficient just to add the folder where all important pg_ files live. For postgres.app this path on Mac OS X is /Applications/Postgres.app/Contents/Versions/9.3/bin. PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.3/bin Please update your version* if a new version got released!
This worked for me on OS X El Capitan: PATH=$PATH:/usr/local/Cellar/postgresql/9.5.0/bin/
o
olicarbo

Ali's solution worked for me but I was having trouble finding the bin folder location. A quick way to find the path on Mac OS X is to open psql (there's a quick link in the top menu bar). This will open a separate terminal window and on the second line the path of your Postgres installation will appear like so:

My-MacBook-Pro:~ Me$ /Applications/Postgres93.app/Contents/MacOS/bin/psql ; exit;

Your pg_config file is in that bin folder. Therefore, before installing psycopg2 set the path of the pg_config file:

PATH=$PATH:/Applications/Postgres93.app/Contents/MacOS/bin/

or for newer version:

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

Then install psycopg2.


Newer docs use the following path: /Applications/Postgres.app/Contents/Versions/9.3/bin
It does seem to move about. find /usr/local -name 'pg_config'
D
DUDANF

I'm going to leave this here for the next unfortunate soul who can't get around this problem despite all the provided solutions. Simply use sudo pip3 install psycopg2-binary


This didn't work for me. Only brew install postgresql fixed it.
You might have to pip install psycopg2-binary for me: pip -> python2, pip3 -> python3. But so long as you got it working, it was one of many solutions!
Solved my problem when using docker.
A
Aswin

You need to upgrade your pip before installing psycopg2. Use this command

pip install --upgrade pip

A more detailed answer could be helpful in most cases.
C
C--

On MacOS, the simplest solution will be to symlink the correct binary, that is under the Postgres package.

sudo ln -s /Applications/Postgres.app/Contents/Versions/latest/bin/pg_config /usr/local/bin/pg_config

This is fairly harmless, and all the applications will be able to use it system wide, if required.


Works for me, did not have to sudo though. Thank you!
Works for me indeed!
On macOS 11.01, M1 Chip, creating a symlink worked for me: sudo ln -s /Library/PostgreSQL/13/bin/pg_config /usr/local/bin/pg_config
D
Dinesh Sunny

On Mac OS X and If you are using Postgres App (http://postgresapp.com/):

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

No need to specify version of Postgres in this command. It will be always pointed to latest.

and do

pip install psycopg2

P.S: If Changes doesn't reflect you may need to restart the Terminal/Command prompt

Source


R
Ronan Boiteau

Installing python-psycopg2 solved it for me on Arch Linux:

pacman -S python-psycopg2

What about Windows?
@PrathameshMore I don't know, I don't use Windows. Have you tried this answer: stackoverflow.com/a/25767749/5660517 ?
a
azalea

On Windows, You may want to install the Windows port of Psycopg, which is recommended in psycopg's documentation.


M
Morfat Mosoti

Just solved the problem in Cent OS 7 by:

export PATH=$PATH:/usr/pgsql-9.5/bin

make sure your PostgreSql version matches the right version above.


K
Kobi Barac

This was partly suggested before, adding it here for clarity.

From the documentation at https://www.psycopg.org/docs/install.html.
they suggest running: $ pip install psycopg2-binary

That solved the issue for me.


G
Gabi

Here, for OS X completeness: if you install PostgreSQL from MacPorts, pg_config will be in /opt/local/lib/postgresql94/bin/pg_config.

When you installed MacPorts, it already added /opt/local/bin to your PATH.

So, this will fix the problem: $ sudo ln -s /opt/local/lib/postgresql94/bin/pg_config /opt/local/bin/pg_config

Now pip install psycopg2 will be able to run pg_config without issues.


Thank you for providing a MacPorts-specific answer! 🙏🏻 I could not find one, and this worked for me.
q
qarthandso

To those on macOS Catalina using the zsh shell who have also installed the postgres app:

Open your ~/.zshrc file, and add the following line:

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

Then close all your terminals, reopen them, and you'll have resolved your problem.

If you don't want to close your terminals, simply enter source ~/.zshrc in whatever terminal you'd like to keep working on.


this works on big sur too.

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now