# Error: pg_config executable not found (psycopg2): how to fix it

> Error: pg_config executable not found, followed by Failed building wheel for psycopg2, means pip is compiling psycopg2 from source and the machine has no PostgreSQL client headers or pg_config. Switch to a package that needs no compiling, psycopg2-binary or psycopg[binary] for Psycopg 3, or install libpq-dev and a C compiler where the build runs.

Source: https://livops.dev/en/guides/error-psycopg2-pg-config-not-found

By Livops team, the people who build and run Livops. Updated 1 December 2026.

This is what psycopg2 itself prints when the build cannot find `pg_config`[1]:

```text
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.
```

pip prints it inside its own error block. When the compile step itself fails, for example on a missing header, pip's summary line is `ERROR: Failed building wheel for psycopg2`[4]. Same cause, same fixes. This page explains why psycopg2 compiles at all, gives the three fixes in the order we would try them, and says what Livops does with it.

## What does the error mean?

`psycopg2` is published as a source package: `pip install psycopg2` compiles it, and that needs build prerequisites[2]:

- a C compiler;
- the Python header files;
- the libpq header files (from `libpq-dev`);
- the `pg_config` program, usually installed by `libpq-dev` but sometimes not on the `PATH`[2].

`pg_config` is needed only to compile psycopg2, not to run it[2]. A slim Docker image, a fresh CI runner or a minimal server has none of these, so the build stops at the first one it looks for. The messages for the others are just as specific[2]:

```text
error: Python.h: No such file or directory      # Python headers missing
error: libpq-fe.h: No such file or directory    # libpq headers missing (libpq-dev)
```

## Fix 1: use psycopg2-binary

The binary package is pre-built and needs none of the build or runtime prerequisites[2]. In `requirements.txt`, replace the line:

```text
psycopg2-binary>=2.9    # instead of psycopg2
```

Your code does not change: it still says `import psycopg2`.

Know the trade-off the psycopg2 docs state. The binary package is meant for getting started; it bundles its own versions of `libpq` and `libssl`, which upgrading the system libraries does not upgrade, and the docs advise the source distribution for production use[2]. They also advise against making `psycopg2-binary` a dependency of a package you publish[2].

## Fix 2: Psycopg 3 with the binary extra

For new code, Psycopg 3 is installed as `psycopg`, and its binary installation is the quickest route: precompiled C extensions packaged with the client libraries, recommended for most users, on Linux, macOS and Windows[3]:

```text
psycopg[binary]>=3.2
```

The import is `import psycopg`. Psycopg 3 tries to behave as closely as possible to psycopg2, with a few differences to be aware of[6], so read them before you move existing code. Psycopg 3 has two other installs[3]:

- **plain `psycopg`**: pure Python, needs no compiler but needs the system `libpq` (on Debian, `libpq5`), and is much slower than the binary or local install;
- **`psycopg[c]`**: compiled locally against the system libraries. It needs the same prerequisites as psycopg2: a C compiler, `python3-dev`, `libpq-dev` and `pg_config` on the `PATH`. Psycopg calls this the preferred install for a production site, because it links to system libraries that can be upgraded.

**A new Python version may have no binary yet.** Binary packages for a new Python appear only once Psycopg's build runners support it[3]. Until then pip uses the source package and the compile error comes back. Pin the Python version your binary has wheels for, or ask pip to refuse source packages so the gap shows up as a clear error[5]:

```sh
pip install --only-binary psycopg2-binary -r requirements.txt
```

## Fix 3: install the build tools in a Dockerfile

When you want the source package (Psycopg's own advice for production), give the build what it asks for: a C compiler and `libpq-dev`, which brings the libpq headers and usually `pg_config`[2]. On a Debian-based image:

```dockerfile
FROM python:3.12-slim

RUN apt-get update \
    && apt-get install -y --no-install-recommends gcc libpq-dev \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
```

If `pg_config` is installed but not found, check it with `pg_config --version`; psycopg2's message above says to add its directory to the `PATH`[1][2].

## How to confirm the fix locally

Install your requirements in a clean container with the Python you deploy on, and nothing else installed:

```sh
docker run --rm -v "$PWD":/app -w /app python:3.12-slim \
  pip install -r requirements.txt
```

With `psycopg2-binary` or `psycopg[binary]`, it should install wheels and compile nothing. If it still prints `pg_config executable not found`, something still pulls in plain `psycopg2`: a line in the file, or a package that depends on it. `grep -i psycopg requirements.txt` finds the first kind.

## On Livops

- **The install.** Livops installs `requirements.txt` with pip into the project's `.venv`; you do not upload `.venv` yourself.
- **A retry with a compiler.** When a package such as `psycopg2` or `mysqlclient` fails to compile, Livops retries the install once in an image with a compiler. If that still fails, switch to the variant that needs no compiling: `psycopg2-binary` or `psycopg[binary]`.
- **The Tip line.** When the log says `pg_config executable not found`, it ends with a **Tip:** line: a package compiles C code on install; Livops retries with a compiler, and if that still fails, use `psycopg[binary]` or `psycopg2-binary`.
- **When it still fails**, the deploy stops as `install_failed` and the old version keeps running. Next to the failed build, the assistant's button reads the log and explains it. See [A package compiles on install (Python)](https://livops.dev/en/docs/error-install#py-compiler).
- **Choose the Python.** Livops runs Python 3.10 to 3.14. A `.python-version` file at the project root pins it:

  ```text
  3.12
  ```

- **Your own Dockerfile.** A Dockerfile in the repository is used when no framework is recognised. To build a recognised Python project from its Dockerfile instead (for Fix 3), add `"builder": "dockerfile"` to `livops.json` before the project is created:

  ```json
  {
    "builder": "dockerfile"
  }
  ```

- **Start commands use the project's environment.** A start command you write yourself begins with `.venv/bin/`, for example `.venv/bin/python main.py`.

The general case, any package that fails to build a wheel, is in [pip: Failed building wheel](https://livops.dev/en/guides/error-pip-failed-building-wheel).

## Questions

**What is pg_config and why does pip need it?**

pg_config is a small program that comes with the PostgreSQL client development files and tells a build where libpq and its headers are. psycopg2 needs it only to compile, not to run.

**Is psycopg2-binary safe for production?**

The psycopg2 docs present it as the package for getting started and advise the source distribution for production, because the binary bundles its own libpq and libssl that system updates do not touch. Many projects still run it where compiling is not possible; weigh that trade-off yourself.

**Should I move to Psycopg 3?**

For new code it is the simpler route: pip install psycopg[binary] needs no compiler, and Psycopg 3 recommends it for most users. It tries to behave as closely as possible to psycopg2 but has a few differences, so read them before moving existing code.

**Do I need PostgreSQL installed to use psycopg2?**

Not the server. Compiling psycopg2 needs the client headers and pg_config, usually from the libpq-dev package. psycopg2-binary needs neither.

**Why does it install on my laptop but fail in Docker?**

Your laptop has PostgreSQL's client tools, or pip found a wheel there. A slim Docker image has no compiler and no libpq headers, so a source build of psycopg2 stops at pg_config.

**Which Python versions does Livops run?**

Python 3.10 to 3.14. Pin one with a .python-version file at the project root, for example 3.12.

## Sources

1. [psycopg2 source — setup.py](https://raw.githubusercontent.com/psycopg/psycopg2/master/setup.py). Checked 1 December 2026.
2. [Psycopg 2 — Installation](https://www.psycopg.org/docs/install.html). Checked 1 December 2026.
3. [Psycopg 3 — Installation](https://www.psycopg.org/psycopg3/docs/basic/install.html). Checked 1 December 2026.
4. [pip source — src/pip/_internal/operations/build/wheel.py](https://raw.githubusercontent.com/pypa/pip/main/src/pip/_internal/operations/build/wheel.py). Checked 1 December 2026.
5. [pip docs — pip install](https://pip.pypa.io/en/stable/cli/pip_install/). Checked 1 December 2026.
6. [Psycopg 3 — Differences from psycopg2](https://www.psycopg.org/psycopg3/docs/basic/from_pg2.html). Checked 1 December 2026.
