Mac下解决pg_config not found错误

3 minute read

错误描述

使用Python连接postgresql或者greenplum时,psycopg2(或者预编译版psycopg-binary)是常用的库,当Mac下没有安装postgresql数据库时,安装会出现以下错误:

Collecting psycopg2-binary
  Using cached psycopg2-binary-2.9.3.tar.gz (380 kB)
  Preparing metadata (setup.py) ... error
  error: subprocess-exited-with-error

  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ╰─> [25 lines of output]
      /Users/fanlintao/Library/Caches/pypoetry/virtualenvs/gptest-H6wLnADc-py3.10/lib/python3.10/site-packages/setuptools/config/setupcfg.py:463: SetuptoolsDeprecationWarning: The license_file parameter is deprecated, use license_files instead.
        warnings.warn(msg, warning_class)
      running egg_info
      creating /private/var/folders/4h/js0h6g6975j5qm94fvr26fkc0000gn/T/pip-pip-egg-info-f_tcwfft/psycopg2_binary.egg-info
      writing /private/var/folders/4h/js0h6g6975j5qm94fvr26fkc0000gn/T/pip-pip-egg-info-f_tcwfft/psycopg2_binary.egg-info/PKG-INFO
      writing dependency_links to /private/var/folders/4h/js0h6g6975j5qm94fvr26fkc0000gn/T/pip-pip-egg-info-f_tcwfft/psycopg2_binary.egg-info/dependency_links.txt
      writing top-level names to /private/var/folders/4h/js0h6g6975j5qm94fvr26fkc0000gn/T/pip-pip-egg-info-f_tcwfft/psycopg2_binary.egg-info/top_level.txt
      writing manifest file '/private/var/folders/4h/js0h6g6975j5qm94fvr26fkc0000gn/T/pip-pip-egg-info-f_tcwfft/psycopg2_binary.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.
WARNING: There was an error checking the latest version of pip.

搜索解决方法,大多数都是让安装完整的postgresql数据库,这不优雅,我们很多时候并不需要完整的数据库,仅仅因为一个配置文件而安装上数据库未免太杀鸡用牛刀。

解决方法

仅需要安装上libpq包即可, 如下:

brew install libpq
# 安装完成后
echo 'export PATH="/opt/homebrew/opt/libpq/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc

然后再安装psycopg2(或psycopg2-binary)

pip install psycopg2-binary

其他错误

在安装psycopg2时,可能还会遇到以下错误(截取关键日志),原因是缺少相关依赖。

  ...    
      ld: library not found for -lssl
      clang: error: linker command failed with exit code 1 (use -v to see invocation)
      error: command '/usr/bin/clang' failed with exit code 1
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: legacy-install-failure

× Encountered error while trying to install package.
╰─> psycopg2-binary

安装openssl

brew install openssl
echo 'export LDFLAGS="-L/opt/homebrew/opt/openssl@3/lib"' >> ~/.zshrc
source ~/.zshrc