この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
動作確認環境
- MacOS Catalina: 10.15.6
- postgresql: 12.4
- psycopg2: 2.8.5
手順
postgresqlが入ってない場合はHomebrewでインストールしておきます。
$ brew install postgresql
続いてpsycopg2を入れようとすると怒られます。
$ pipenv install psycopg2
Installing psycopg2…
Error: An error occurred while installing psycopg2!
Error text: Collecting psycopg2
Using cached psycopg2-2.8.5.tar.gz (380 kB)
ERROR: Command errored out with exit status 1:
command: /Users/pass-to-project/.venv/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/1j/5sjwl16d5hj021czz_kbq03w0000gq/T/pip-install-9ddgli06/psycopg2/setup.py'"'"';
[中略]
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>).
----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
This is likely caused by a bug in psycopg2. Report this to its maintainers.
✘ Installation Failed
対処法
psycopg2をインストールする前に以下を実行します。
opensslが入っていない場合はインストールします。
$ brew install openssl
環境変数 LDFLAGS
, CPPFLAGS
の2つを設定します。
$ export LDFLAGS="-L/usr/local/opt/openssl/lib"
$ export CPPFLAGS="-I/usr/local/opt/openssl/include"
再度インストール
$ pipenv install psycopg2
Installing psycopg2…
✔ Success!
無事入れられました。
ここではpipenvを使っていますが、pipでも同じです。