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.

1 Like

Thank you!

I had the same problem.
I searched for a solution and found the following tweet

By putting psycopg2-binary instead of psycopg2 in requirements.txt, I was able to deploy successfully using the builder instead of Dockerfile.
(This text is converted from Japanese. Sorry if it is hard to read.)

Note that psycopg2-binary is not intended for production use though: Installation — Psycopg 2.9.4 documentation

As I had same problem, in case anyone else is wondering about this, you can also use paketobuildpacks/builder:full instead of default paketobuildpacks/builder:base.

Full buildpack seems to include required dependencies for building psycopg2.

2 Likes

For future reference, if you had an auto-generated Dockerfile from using fly launch, you can get the right dependencies in place by changing the line that has PYTHON_VERSION=[version]-slim-[linux flavor] to PYTHON_VERSION=[version]-[linux flavor].

For example, mine was PYTHON_VERSION=3.10-slim-buster and I changed it to PYTHON_VERSION=3.10-buster and things are working now.

1 Like

Hi Shugel. The first part f your explanation has worked. But generated a 1gb file that caused a timeout in my server.
I think the problem is with the second part, about fly.toml alterations…
Could You give me an example or explain with more details?