如何安装psycopg2并解决pg_config错误?

84
我曾尝试从这个网站安装psycopg2(PostgreSQL数据库适配器),但当我进入包并写下安装命令时,遇到了问题。
python setup.py install 

我遇到了以下错误:

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

我也尝试了 'sudo pip install psycopg2',得到了相同的消息。

阅读完文档后,它要求查看setup.cfg文件(如下所示):

[build_ext]
define=

# PSYCOPG_DISPLAY_SIZE enable display size calculation (a little slower)
# HAVE_PQFREEMEM should be defined on PostgreSQL >= 7.4
# PSYCOPG_DEBUG can be added to enable verbose debug information

# "pg_config" is required to locate PostgreSQL headers and libraries needed to
# build psycopg2. If pg_config is not in the path or is installed under a
# different name uncomment the following option and set it to the pg_config
# full path.
#pg_config=

# Set to 1 to use Python datetime objects for default date/time representation.
use_pydatetime=1

# If the build system does not find the mx.DateTime headers, try
# uncommenting the following line and setting its value to the right path.
#mx_include_dir=

# For Windows only:
# Set to 1 if the PostgreSQL library was built with OpenSSL.
# Required to link in OpenSSL libraries and dependencies.
have_ssl=0

# Statically link against the postgresql client library.
#static_libpq=1

# Add here eventual extra libraries required to link the module.
#libraries=

不过,我不确定我是否应该编辑这个文件,因为文档中写着:

then take a look at the setup.cfg file.

Some of the options available in setup.cfg are also available as command line arguments of the build_ext sub-command. For instance you can specify an alternate pg_config version using:

$ python setup.py build_ext --pg-config /path/to/pg_config build

Use python setup.py build_ext --help to get a list of the options supported.
我已经获得了支持的选项列表,但我不确定接下来该怎么做。

这个回答解决了你的问题吗?pg_config可执行文件未找到 - Gonçalo Peres
15个回答

119
Debian/Ubuntu

Python 2

:Python编程语言的一个旧版本。

sudo apt install libpq-dev python-dev

Python 3

sudo apt install libpq-dev python3-dev

额外信息

If none of the above solve your issue, try

sudo apt install build-essential
or

sudo apt install postgresql-server-dev-all

使用pip安装

安装psycopg2-binary PyPI包,该包包含适用于Linux和Mac OS的Python Wheels。

pip install psycopg2-binary

3
我也遇到了这个问题。不幸的是,使用我的软件包时安装python3-dev不起作用。我所做的是对此答案进行排列组合:
  1. sudo pip install --upgrade pip
  2. sudo apt install -y libpq-dev postgresql-server-dev-all
  3. sudo pip3 install psycopg2 不确定是否所有的sudo都是必需的,但我不在乎,所以我加上它们了。
- Intrastellar Explorer
1
对于虚拟环境,pip install psycopg2-binary 是解决方案。 - YohanK
我有Ubuntu,这个“sudo apt install libpq-dev python3-dev”对我来说可以正常工作,但我认为这不是正确的方法,因为它并不专门针对我的Virtualenv。 - sami

53

我遇到了这个问题,因为我还没有在我的计算机上安装PostgreSQL。

对于Mac

brew install postgresql

对于没有安装brew的Mac电脑

python3 -m pip install psycopg2-binary

1
是的,它确实做到了。 - Stiven Ramírez Arango
1
这在我的MacOS Big Sur上有效。 - Mohit Munjal
1
经过两天的搜索,这个方法对我有用,谢谢。 - lolu

27

如果您需要无需编译安装:

pip install psycopg2-binary

https://www.psycopg.org/docs/install.html#binary-install-from-pypi

注意: psycopg2-binary 软件包适用于初学者开始使用 Python 和 PostgreSQL,无需满足构建要求即可进行操作。如果您是依赖 psycopg2 的发布软件包的维护人员,则不应将 'psycopg2-binary' 用作模块依赖项。对于生产环境,请使用源代码分发。


1
干净快速的解决方案。它不需要任何其他库的安装。谢谢。 - rogervila
5
从psycopg2的文档中(https://www.psycopg.org/docs/install.html#binary-install-from-pypi)可以了解到:注意:psycopg2-binary软件包适用于初学者,可以开始使用Python和PostgreSQL而无需满足构建要求。 如果您是依赖于psycopg2的发布软件包的维护者,则不应将'psycopg2-binary'用作模块依赖项。建议在生产中使用源分发版。 请将此内容添加到您的答案中。 - Koen van Zuijlen
作为开发机器的最佳选择,无需像其他答案建议的那样安装完整的PostgreSQL软件包。 - ggat

17

如果您使用的是Ubuntu或其他基于Debian的发行版,请尝试

sudo apt-get install python3-psycopg2
否则,您需要查找并安装适用于您发行版的Postgresql客户端软件包。从源代码安装psycopg2

你如何使用 requirements.txt 来标准化安装过程? - Kannan Ramamoorthy
1
我正在使用Ubuntu Buster,安装了python-psycopg2包,但仍然无法正常工作。顺便说一下,这个包是为Python 2.x设计的,对于Python 3.x,包名应该是python3-psycopg2。但是两者都不行。我正在运行Python 3.8。 - Devy
这几乎可以确定不是解决问题的正确方式。你永远不应该以root身份安装pip软件包,唯一的例外是Docker容器,并且你知道自己在做什么,即使在这种情况下,最好还是创建一个进程用户。 - DeusXMachina

5

在搭载 M1 芯片的 Macbook 上,我需要安装 PostgreSQL 数据库。

brew install postgresql

如果您没有brew:https://brew.sh,然后再次运行安装。
python3 -m pip install psycopg2-binary

3

通过升级pip解决了我的问题:pip install --upgrade pip


1
如果您在macOS上安装了Postgres.app,请将其bin目录添加到PATH环境变量中:
export PATH=/Applications/Postgres.app/Contents/Versions/latest/bin:$PATH

1

在 Fedora 上 (已在 Fedora 35 上进行测试并从源代码构建)

pg_config 存在于 libpq5-devel 中。 现在尝试以下步骤:

sudo dnf install libpq5-devel

python setup.py build
sudo python setup.py install

我在我的 Fedora 35 上找不到那个库。通过执行 "sudo dnf search libpq" 命令,我找到了一个名为 libpq-devel.x86_64 的库,其中包含 "pg_config" 可执行文件。 - Kiteloopdesign

1
对于使用Macports的OSX,您可以安装sudo port install py38-psycopg2来安装Python 3.8。您可以使用port search psycopg2搜索您的Python版本。撰写本文时,版本范围从2.7到3.9,均更新为psycopg2的2.8.6版本。
注意:这可能无法帮助venv。
编辑:因此,要找到venv的pg_config,请运行命令port contents postgresql13 | grep pg_config,其中postgresql13是从上面的端口命令安装的postgresql软件包的版本。您正在寻找诸如/opt/local/lib/postgresql13/bin/pg_config之类的路径。使用诸如export PATH=/opt/local/lib/postgresql13/bin/:$PATH的命令将其导出到您的PATH变量中。
有关Homebrew,请参考this answer

0
如果您正在使用PyCharm/Visual Code Studio终端,请不要这样做。请使用默认的系统终端,因为它可以确保正确执行brew install的格式。
在搭载Intel芯片的M1 Pro上: brew install postgresql pip install psycopg2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接