Can't install psycopg2 due to missing pg_config

I’m trying to deploy a Flask app, but the dependency install step is failing:

Collecting psycopg2==2.9.3
  Downloading psycopg2-2.9.3.tar.gz (380 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 380.6/380.6 kB 75.0 MB/s eta 0:00:00
  Preparing metadata (setup.py): started
  Preparing metadata (setup.py): finished with status 'error'
  error: subprocess-exited-with-error
  
  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ╰─> [25 lines of output]
      /layers/paketo-buildpacks_cpython/cpython/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 /tmp/pip-pip-egg-info-ydcnhb7u/psycopg2.egg-info
      writing /tmp/pip-pip-egg-info-ydcnhb7u/psycopg2.egg-info/PKG-INFO
      writing dependency_links to /tmp/pip-pip-egg-info-ydcnhb7u/psycopg2.egg-info/dependency_links.txt
      writing top-level names to /tmp/pip-pip-egg-info-ydcnhb7u/psycopg2.egg-info/top_level.txt
      writing manifest file '/tmp/pip-pip-egg-info-ydcnhb7u/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]

The fly.toml has the following (auto-detected) build directive:

[build]
  builder = "paketobuildpacks/builder:base"

Should that work, or do I need to specify something different / python-specific?

I’m having the same issue with my app.
Just for more information: I’m using python 3.8, SQLAlchemy, Flask, PostgreSQL 12 on Ubuntu in WSL, running a virtual environment.

Any help would be much appreciated!!

I gave up on using a builder and switched to a Dockerfile instead – I was getting unexplained timeouts when I specified a Paketo buildpack and the docs are pretty vague, so I Dockerised the app instead:

FROM python:alpine
RUN apk update \
    && apk add libpq postgresql-dev \
    && apk add build-base
COPY ./requirements.txt /app/requirements.txt
WORKDIR /app
RUN pip install -r requirements.txt
COPY . /app
CMD ["gunicorn", "-b", "0.0.0.0:8080", "--workers", "3", "app:app"]

Be sure to edit the top-level [build] section from your fly.toml to remove the builder and buildpacks keys and use the dockerfile by using the dockerfile = "Dockerfile" key, and ensure the PORT key in [env] matches the port you specify in your Dockerfile CMD. Note also that you’ll need to tune the workers setting in your Dockerfile to match the number of CPUs in your app – the rule of thumb is num CPUs * 2 + 1.

Thank you!