psycopg2-binary vs psycopg2: 'pg_config executable not found' on Python 3.12
Our deploy script started failing at 2am on a fresh Ubuntu 22.04 runner, right after we bumped the base image. Everything had worked on the old box. The relevant part of the log:
Collecting psycopg2==2.9.9
Using cached psycopg2-2.9.9.tar.gz (384 kB)
Preparing metadata (setup.py) ... error
error: subprocess-exited-with-error × Preparing metadata (setup.py) did not run successfully.
│ exit code: 1
╰─> [8 lines of output]
running egg_info
writing psycopg2.egg-info/PKG-INFO
creating psycopg2.egg-info
writing top-level names to psycopg2.egg-info/top_level.txt
writing requirements to psycopg2.egg-info/requires.txt
writing manifest file 'psycopg2.egg-info/SOURCES.txt'
Error: pg_config executable not found.
ERROR: Failed building wheel for psycopg2
pg_config executable not found. The box had no PostgreSQL client headers, because the old image happened to have them and the new one didn't.
## Why the source build needs pg_config
psycopg2 is a C extension. When pip can't find a matching wheel it downloads the sdist and compiles it, and the build script shells out to pg_config to discover include and library paths. No pg_config, no build. That's the whole error.
The wheel situation changed across versions. psycopg2-binary ships manylinux wheels with libpq bundled, so it doesn't need pg_config at all. Plain psycopg2 has wheels too on some platforms, but on aarch64 and a lot of Alpine images you fall back to source.
## The two-minute fix
On Ubuntu/Debian, install the dev package:
bash
sudo apt-get update
sudo apt-get install -y libpq-dev python3-dev
pip install psycopg2==2.9.9
libpq-dev provides pg_config; python3-dev provides Python.h. Missing either one gives you a different message — no Python.h means fatal error: Python.h: No such file or directory. We needed both.
On Alpine it's apk add libpq-dev python3-dev plus build-base, because there's no gcc by default. On macOS, brew install postgresql@16 puts pg_config on the PATH for you, though you may need to export the keg-only bin directory first.
You can confirm what pip is doing with pip install -v. When it prints "Using cached psycopg2-2.9.9.tar.gz" instead of a .whl filename, you already know it's going to compile, so save yourself the wait and install the headers first.
## When the binary wheel is actually fine
There's a long-running debate about psycopg2-binary being "not for production". The packaging docs say it bundles its own libpq and openssl, which can drift from the system ones. Fair. For a small service where you don't want a compiler in the runtime image, I'll take that tradeoff every time:
bash
pip install psycopg2-binary==2.9.9
The thing I would not do is pin psycopg2-binary in a library's install_requires, because it changes what gets installed for everyone downstream. In an application's requirements file, though, it's fine.
## What we ended up doing
We split it: the build stage installs libpq-dev and compiles psycopg2, the runtime stage copies the wheel in. Multi-stage Docker, compiler stays out of the final image. It's about six more lines in the Dockerfile and it stopped the 2am surprises.
If you're on psycopg 3 already, none of this applies — psycopg[binary] handles it differently. But for a codebase still on 2.9.x, pg_config is going to bite you eventually on a new machine.