我在安装psycopg2时遇到了麻烦。当我尝试pip安装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

但问题是pg_config实际上在我的PATH中;它运行起来没有任何问题:

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

我尝试将pg_config路径添加到setup.cfg文件中,并使用我从他们的网站(http://initd.org/psycopg/)下载的源文件构建它,我得到了以下错误消息!

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

但它确实在那里!!

我对这些错误感到困惑。有人能帮忙吗?

顺便说一下,我sudo所有的命令。而且我使用的是RHEL 5.5。


当前回答

阿里的解决方案为我工作,但我有麻烦找到bin文件夹的位置。在Mac OS X上找到路径的一个快速方法是打开psql(在顶部菜单栏中有一个快速链接)。这将打开一个单独的终端窗口,在第二行中,Postgres的安装路径将如下所示:

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

您的pg_config文件在bin文件夹中。因此,在安装psycopg2之前,设置pg_config文件的路径:

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

或更新版本:

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

然后安装psycopg2。

其他回答

对于windows用户来说,这个问题缺乏答案。下面是我解决这个问题的方法。

首先像这样将PostgreSQL bin文件夹添加到路径变量中

set PATH=%PATH%;C:\Program Files\PostgreSQL\11\bin

用你的Postgres版本替换\11\

下一个运行

pip install pyscopg2

这至少让我出现了一个编译错误。

更新:它看起来是psycopg2库的一个问题,它的依赖关系在python 3.8中表现不太好。https://github.com/psycopg/psycopg2/issues/990

对于ubuntu用户,这是解决方案:

sudo apt install libpq-dev

这对我很管用。

刚刚解决了问题在Cent OS 7:

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

确保你的PostgreSql版本与上面的正确版本匹配。

这就是我安装psycopg2的方法

$ wget http://initd.org/psycopg/tarballs/PSYCOPG-2-5/psycopg2-2.5.3.tar.gz
$ tar -xzf psycopg2-2.5.3.tar.gz
$ cd psycopg2-2.5.3
$ pip install .

我遇到这个问题是因为我没有安装postgres。如果你有brew安装运行

brew install postgresql

这应该可以解决问题。