Error: pg_config executable not found.

文章讲述了用户在尝试使用pip安装psycopg2时遇到的问题,因为缺少本地PostgreSQL数据库。文章提供了解决方案,即安装psycopg2-binary包,避免源码编译。

使用 pip 安装 psycopg2 失败,原因是本地没有安装 postgres 数据库

>>> pip install psycopg2 -i https://pypi.tuna.tsinghua.edu.cn/simple/
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/
Collecting psycopg2
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/c9/5e/dc6acaf46d78979d6b03458b7a1618a68e152a6776fce95daac5e0f0301b/psycopg2-2.9.9.tar.gz (384 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 384.9/384.9 kB 1.0 MB/s eta 0:00:00
  Preparing metadata (setup.py) ... error
  error: subprocess-exited-with-error
  
  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ╰─> [23 lines of output]
      running egg_info
      creating /tmp/pip-pip-egg-info-mn6hq8nn/psycopg2.egg-info
      writing /tmp/pip-pip-egg-info-mn6hq8nn/psycopg2.egg-info/PKG-INFO
      writing dependency_links to /tmp/pip-pip-egg-info-mn6hq8nn/psycopg2.egg-info/dependency_links.txt
      writing top-level names to /tmp/pip-pip-egg-info-mn6hq8nn/psycopg2.egg-info/top_level.txt
      writing manifest file '/tmp/pip-pip-egg-info-mn6hq8nn/psycopg2.egg-info/SOURCES.txt'
      
      Error: pg_config executable not found.
      
      pg_config is required to build psycopg2 from source.  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'.
      
      If you prefer to avoid building psycopg2 from source, please install the PyPI
      'psycopg2-binary' package instead.
      
      For further information please check the 'doc/src/install.rst' file (also at
      <https://www.psycopg.org/docs/install.html>).
      
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.

可是朕只想装个客户端去连接远程的postgres,看见有的解决方案是先本地装个 postgresql,朕不答应。

仔细看上面的报错说明,其实已经给了解决方案:

If you prefer to avoid building psycopg2 from source, please install the PyPI
‘psycopg2-binary’ package instead.

>>> pip install psycopg2-binary -i https://pypi.tuna.tsinghua.edu.cn/simple/
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/
Collecting psycopg2-binary
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/19/7a/e806ed82d954b9ec29b62f12ae8929da8910cde5ab7e919ec0983e56672d/psycopg2_binary-2.9.9-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.0 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.0/3.0 MB 940.3 kB/s eta 0:00:00
Installing collected packages: psycopg2-binary
Successfully installed psycopg2-binary-2.9.9

朕很满意

<think>根据用户的问题和提供的引用内容,用户遇到了安装psycopg2时出现的"pg_config executable not found"错误。pg_configPostgreSQL的一个工具,用于获取已安装PostgreSQL的信息,psycopg2在编译安装时需要这个工具。 解决方案: 1. **确保PostgreSQL已安装**:因为pg_configPostgreSQL的一部分,所以首先需要确认系统上已经安装PostgreSQL。如果没有安装,需要先安装PostgreSQL。 2. **将pg_config所在目录加入PATH**: 在Linux/macOS上,可以通过以下命令临时添加(假设PostgreSQL安装在默认位置): ```bash export PATH=$PATH:/usr/local/pgsql/bin ``` 或者永久添加,将上述命令添加到`~/.bashrc`或`~/.zshrc`中。 3. **在安装时指定pg_config路径**: 如果知道pg_config的具体路径,可以在安装psycopg2时指定: ```bash pip install psycopg2 --global-option=build_ext --global-option="-I/usr/local/pgsql/include" --global-option="-L/usr/local/pgsql/lib" ``` 或者使用setup.cfg文件配置,但通常使用命令行参数更方便。 4. **使用psycopg2-binary(推荐)**: 对于开发环境,可以使用预编译的二进制包psycopg2-binary,它不需要编译,因此也就无需pg_config: ```bash pip install psycopg2-binary ``` 注意:官方文档建议在生产环境中使用psycopg2(需要编译),但开发环境可以用binary版。 5. **安装PostgreSQL开发包(Linux)**: 在基于Debian的系统(如Ubuntu)上,可以安装libpq-dev包,它提供了pg_config: ```bash sudo apt-get install libpq-dev ``` 在基于RPM的系统(如CentOS)上,安装postgresql-devel: ```bash sudo yum install postgresql-devel ``` 6. **macOS特定解决方案**: 在macOS上,如果使用Homebrew安装PostgreSQL,那么pg_config应该已经在PATH中。如果没有,可以尝试重新链接: ```bash brew install postgresql brew link postgresql --force ``` 或者,如果已经安装,可以尝试: ```bash brew reinstall postgresql ``` 然后安装psycopg2-binary(因为macOS的Python环境可能缺少dev支持): ```bash pip install psycopg2-binary ``` 7. **Windows系统**: 在Windows上,通常安装完整的PostgreSQL安装包(如EnterpriseDB提供的安装包)会包含pg_config,并会自动添加到PATH。如果没有,可以手动将PostgreSQL的bin目录(如`C:\Program Files\PostgreSQL\14\bin`)添加到系统环境变量PATH中。 8. **验证pg_config是否可用**: 安装完成后,在终端运行: ```bash pg_config --version ``` 如果能够输出PostgreSQL的版本信息,则说明pg_config已经可用。 9. **重新安装psycopg2**: 在确保pg_config可用后,重新安装psycopg2: ```bash pip uninstall psycopg2 pip install psycopg2 ``` 注意:根据引用[2],在macOS上,推荐使用psycopg2-binary,因为mac的Python通常没有安装python-dev,而psycopg2-binary是预编译的二进制包,避免了编译依赖。 总结步骤: - 安装PostgreSQL(如果未安装)或安装对应的开发包(如libpq-dev) - 确保pg_config在PATH中 - 如果不想处理依赖,使用psycopg2-binary(开发环境推荐) - 重新安装psycopg2 最后,根据用户的问题,生成相关问题。</think>### 解决 "pg_config executable not found" 错误的完整指南 当安装 `psycopg2` 时出现 `pg_config executable not found` 错误,表明系统缺少 PostgreSQL 的开发依赖。以下是针对不同操作系统的解决方案: #### 1. **Linux 解决方案(Ubuntu/Debian)** ```bash # 安装 PostgreSQL 开发包 sudo apt-get update sudo apt-get install libpq-dev python3-dev # 验证 pg_config which pg_config # 应输出路径如 /usr/bin/pg_config # 重新安装 psycopg2 pip install psycopg2 ``` #### 2. **macOS 解决方案** ```bash # 通过 Homebrew 安装 PostgreSQL brew install postgresql # 将 pg_config 添加到 PATH(临时) export PATH="/usr/local/opt/postgresql/bin:$PATH" # 永久添加路径(推荐) echo 'export PATH="/usr/local/opt/postgresql/bin:$PATH"' >> ~/.zshrc source ~/.zshrc # 使用二进制版本避免编译问题 [^2] pip install psycopg2-binary ``` #### 3. **Windows 解决方案** 1. 下载 PostgreSQL 安装包:https://www.postgresql.org/download/ 2. 安装时勾选 **Command Line Tools** 3.PostgreSQL 的 bin 目录添加到系统 PATH: - 默认路径:`C:\Program Files\PostgreSQL\15\bin` 4. 在命令提示符验证: ```cmd pg_config --version ``` 5. 安装预编译包: ```cmd pip install psycopg2-binary ``` #### 4. **手动指定 pg_config 路径** 如果知道 `pg_config` 位置但不在 PATH 中: ```bash pip install psycopg2 --global-option=build_ext \ --global-option="-I/usr/local/opt/postgresql/include" \ --global-option="-L/usr/local/opt/postgresql/lib" ``` #### 5. **验证安装** ```python import psycopg2 try: conn = psycopg2.connect("dbname=test user=postgres") print("连接成功!") except Exception as e: print("错误:", e) ``` #### 各操作系统依赖包总结 | 系统 | 所需依赖包 | 验证命令 | |---------|-----------------------------|-------------------| | Ubuntu | `libpq-dev python3-dev` | `dpkg -l libpq-dev` | | CentOS | `postgresql-devel python3-devel` | `rpm -q postgresql-devel` | | macOS | `postgresql` (via Homebrew) | `brew list postgresql` | | Windows | PostgreSQL 安装包 (含CLI工具) | `pg_config --version` | > **关键提示**:开发环境强烈推荐使用 `psycopg2-binary` 可避免 90% 的编译问题[^2]。生产环境建议使用完整编译的 `psycopg2` 并确保 `pg_config` 在 PATH 中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

颹蕭蕭

白嫖?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值