【解決法】Error: pg_config executable not found.

# エラー対処

ライブラリインストール時のエラーを書き留めておきます

### 事前コマンド
```terminal
$ cd ~/対象プロジェクトまでのpath
$ python -m venv env # pythonの実行環境を仮想的に作成(envは名称なので任意でOK)
$ . env/bin/activate # 作成した仮想環境のバッチを実行し、仮想環境への切り替えを行う
```
```requirements.txt
省略
psycopg2==2.9.3
```

### エラー発生

```terminal
$ pip install -r requirements.txt # 一括でライブラリをインストール
```

```terminal:terminal
(env) (base) sample@SampleMBP 対象プロジェクト % pip install -r requirements.txt
Collecting asgiref==3.2.10
  Using cached asgiref-3.2.10-py3-none-any.whl (19 kB)
Collecting beautifulsoup4==4.11.1
  Using cached beautifulsoup4-4.11.1-py3-none-any.whl (128 kB)
途中省略
Collecting gunicorn==20.1.0
  Using cached gunicorn-20.1.0-py3-none-any.whl (79 kB)
Collecting psycopg2==2.9.1
  Using cached psycopg2-2.9.1.tar.gz (379 kB)
  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 /private/var/folders/yq/492f1qrn6ylcdzjx1mfwbjv40000gn/T/pip-pip-egg-info-7v1vq56z/psycopg2.egg-info
      writing /private/var/folders/yq/492f1qrn6ylcdzjx1mfwbjv40000gn/T/pip-pip-egg-info-7v1vq56z/psycopg2.egg-info/PKG-INFO
      writing dependency_links to /private/var/folders/yq/492f1qrn6ylcdzjx1mfwbjv40000gn/T/pip-pip-egg-info-7v1vq56z/psycopg2.egg-info/dependency_links.txt
      writing top-level names to /private/var/folders/yq/492f1qrn6ylcdzjx1mfwbjv40000gn/T/pip-pip-egg-info-7v1vq56z/psycopg2.egg-info/top_level.txt
      writing manifest file '/private/var/folders/yq/492f1qrn6ylcdzjx1mfwbjv40000gn/T/pip-pip-egg-info-7v1vq56z/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.
```

### Xcodeは事前にインストール済み

```terminal
$ xcode-select --install
```
```termianl
(env) (base) sample@SampleMBP 対象プロジェクト % xcode-select --install
xcode-select: error: command line tools are already installed, use "Software Update" to install updates
```

# 解決法

```terminal
$ brew install postgresql
```

- postgresqlをインストール後に再実行

```termianl:terminal
(env) (base) sample@SampleMBP 対象プロジェクト % pip install -r requirements.txt
Collecting asgiref==3.2.10
  Using cached asgiref-3.2.10-py3-none-any.whl (19 kB)
Collecting beautifulsoup4==4.11.1
  Using cached beautifulsoup4-4.11.1-py3-none-any.whl (128 kB)

途中省略

Collecting dj-database-url>=0.5.0
  Downloading dj_database_url-0.5.0-py2.py3-none-any.whl (5.5 kB)
Collecting pyjwt[crypto]>=1.7
  Downloading PyJWT-2.4.0-py3-none-any.whl (18 kB)
Using legacy 'setup.py install' for django-allauth, since package 'wheel' is not installed.
Using legacy 'setup.py install' for psycopg2, since package 'wheel' is not installed.
Installing collected packages: pytz, django-widget-tweaks, dj-database-url, certifi, whitenoise, urllib3, sqlparse, soupsieve, PyJWT, pycparser, psycopg2, oauthlib, idna, gunicorn, defusedxml, charset-normalizer, asgiref, requests, python3-openid, Django, cffi, beautifulsoup4, requests-oauthlib, django-heroku, django-bootstrap4, cryptography, django-allauth
  Running setup.py install for psycopg2 ... done
  Running setup.py install for django-allauth ... done
Successfully installed Django-3.1.2 PyJWT-2.3.0 asgiref-3.2.10 beautifulsoup4-4.11.1 certifi-2021.10.8 cffi-1.15.0 charset-normalizer-2.0.12 cryptography-36.0.2 defusedxml-0.7.1 dj-database-url-0.5.0 django-allauth-0.50.0 django-bootstrap4-2.3.1 django-heroku-0.3.1 django-widget-tweaks-1.4.8 gunicorn-20.1.0 idna-3.3 oauthlib-3.2.0 psycopg2-2.9.3 pycparser-2.21 python3-openid-3.2.0 pytz-2022.1 requests-2.27.1 requests-oauthlib-1.3.1 soupsieve-2.3.2.post1 sqlparse-0.4.2 urllib3-1.26.9 whitenoise-6.2.0
```

:::note
完了
:::