我在安装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。


当前回答

在安装psycopg2之前,需要升级pip。使用这个命令

pip install --upgrade pip

其他回答

对于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

在Linux Mint sudo apt-get安装libpq-dev为我工作。

你必须安装libpq-dev/ PostgreSQL -libs,这是编译C程序链接到libpq库的头文件和静态库,以便与PostgreSQL数据库后端通信。

在Arch上运行:

$ sudo pacman -S postgresql-libs

在Debian和Ubuntu上:

$ sudo apt-get install libpq-dev

在Mac OS X上:

$ brew install postgresql

在Red Hat/CentOS/Fedora上:

$ sudo yum install postgresql-devel

在MacOS上,最简单的解决方案是将正确的二进制文件进行符号链接,即在Postgres包下。

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

这是相当无害的,如果需要,所有应用程序都可以在系统范围内使用它。

我很确定你也经历过和我一样的“问题”,因此我将为你提供一个极其简单的解决方案……

在你的例子中,你需要添加到$ path(或作为命令参数)的实际路径是:

/usr/pgsql-9.1/bin/pg_config

not

/usr/pgsql-9.1/bin

例如,如果你随后运行python setup.py脚本,你会像这样运行它:

python setup.py build_ext --pg-config /usr/pgsql-9.1/bin/pg_config build

也许太晚了,但仍然是最简单的解决办法。

后来编辑:

在进一步的测试中,我发现如果最初在表单中添加路径到pg_config

/usr/pgsql-9.1/bin

(...../bin后面没有/pg_config)并运行PIP install命令,它将工作。

然而,如果你决定按照指示运行python setup.py,你必须在.....之后用/pg_config指定路径/ bin。

python setup.py build_ext --pg-config /usr/pgsql-9.1/bin/pg_config build