Python Psycog2: How to Resolve "pg_config executable not found" in Python
When working with PostgreSQL databases in Python, the psycopg2 library is the most widely used adapter for database interaction. However, during installation, you may encounter a frustrating error that stops the process entirely:
Error: pg_config executable not found.
This error occurs when pip attempts to compile psycopg2 from source but cannot locate the pg_config executable: a utility that provides the compiler and linker flags needed to build C extensions against the PostgreSQL client library.
In this guide, you'll learn exactly why this error happens and how to fix it on Windows, Linux (Debian/Ubuntu, RHEL/CentOS/Fedora), and macOS.
Why Does This Error Occur?
The psycopg2 package contains C extensions that must be compiled against the PostgreSQL client library. During the build process, the setup script calls pg_config to discover the location of PostgreSQL headers, libraries, and configuration details. If pg_config is missing or not accessible, the build fails immediately.
The most common root causes are:
- PostgreSQL is not installed on your system at all.
- PostgreSQL development headers are missing:
pg_configships with the development package, not the base server or client. pg_configis not on your system'sPATH: the binary exists but Python's build tools can't find it.- You're installing
psycopg2(source version) instead of the precompiledpsycopg2-binary.
Quick Diagnosis
Before applying any fix, check whether pg_config is installed and accessible.
On Linux / macOS:
which pg_config
On Windows (Command Prompt):
where pg_config
- If a path is returned,
pg_configexists. The issue is likely aPATHconfiguration problem. - If nothing is returned, the PostgreSQL development package is not installed.
If you don't need to compile from source and just want things to work, install the precompiled binary version:
pip install psycopg2-binary
This package includes all required PostgreSQL client libraries and does not need pg_config. It's ideal for development, testing, and many production scenarios.
Fixing the Error on Linux (Debian / Ubuntu)
Step 1: Install PostgreSQL Development Headers
The pg_config binary is included in the libpq-dev package, not in the base postgresql package:
sudo apt-get update
sudo apt-get install libpq-dev python3-dev build-essential
If you also need the PostgreSQL server itself (not just the client libraries), add it explicitly:
sudo apt-get install postgresql postgresql-contrib
Step 2: Verify pg_config Is Accessible
which pg_config
Expected output:
/usr/bin/pg_config
Step 3: Install psycopg2
pip install psycopg2
Alternative: Skip Compilation Entirely
pip install psycopg2-binary
Fixing the Error on Linux (RHEL / CentOS / Fedora)
Step 1: Install Development Libraries
RHEL / CentOS:
sudo yum install postgresql-devel python3-devel gcc
Fedora:
sudo dnf install postgresql-devel python3-devel gcc
Step 2: Verify and Install
which pg_config
pip install psycopg2
Fixing the Error on macOS
Step 1: Install PostgreSQL Using Homebrew
brew install postgresql
This installs both the PostgreSQL server and the pg_config utility.
Step 2: Verify the pg_config Path
which pg_config
Expected output (Apple Silicon):
/opt/homebrew/bin/pg_config
Expected output (Intel Mac):
/usr/local/bin/pg_config
Step 3: Add pg_config to PATH (If Not Found)
If which pg_config returns nothing even after installing PostgreSQL via Homebrew, add it to your shell profile manually:
# For zsh (default on modern macOS):
echo 'export PATH="/opt/homebrew/opt/postgresql/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc
# For bash:
echo 'export PATH="/opt/homebrew/opt/postgresql/bin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile
Step 4: Install psycopg2
pip install psycopg2
Alternative: Skip Compilation
pip install psycopg2-binary
Fixing the Error on Windows
Step 1: Install PostgreSQL
Download the installer from the official site: https://www.postgresql.org/download/windows/
During installation, make sure the Command Line Tools (or Development Tools) option is selected.
Step 2: Add pg_config.exe to Your PATH
Locate pg_config.exe. It is typically found at:
C:\Program Files\PostgreSQL\16\bin\pg_config.exe
Add the bin directory to your system PATH:
setx PATH "%PATH%;C:\Program Files\PostgreSQL\16\bin"
After running setx, you must close and reopen your terminal for the change to take effect. The setx command modifies the registry but does not update the current session.
Step 3: Install psycopg2
pip install psycopg2
Alternative: Skip Compilation
pip install psycopg2-binary
This is often the most reliable approach on Windows, as setting up a C compiler toolchain can be complex.
psycopg2 vs. psycopg2-binary: Which Should You Use?
| Feature | psycopg2 | psycopg2-binary |
|---|---|---|
Requires pg_config | Yes | No |
| Requires C compiler | Yes | No |
| Includes bundled libraries | No (links to system libpq) | Yes (self-contained) |
| Recommended for production | ✅ (by psycopg2 maintainers) | Acceptable for most cases |
| Best for development/CI | Works but harder to set up | ✅ Easy, no dependencies |
The psycopg2 maintainers recommend using the source distribution (psycopg2) in production because psycopg2-binary bundles its own copy of libpq and libssl, which may not receive system security updates. For development, testing, and CI/CD pipelines, psycopg2-binary is perfectly fine.
Verifying the Installation
After a successful install, verify that psycopg2 works correctly with a simple connection test:
import psycopg2
try:
connection = psycopg2.connect(
host="127.0.0.1",
port="5432",
database="your_database",
user="your_username",
password="your_password",
)
print("Connection successful!")
cursor = connection.cursor()
cursor.execute("SELECT version();")
version = cursor.fetchone()
print(f"PostgreSQL version: {version[0]}")
except psycopg2.Error as err:
print(f"Error: {err}")
finally:
if "connection" in locals() and connection and not connection.closed:
cursor.close()
connection.close()
print("Connection closed.")
Output:
Connection successful!
PostgreSQL version: PostgreSQL 16.3 on x86_64-pc-linux-gnu, compiled by gcc ...
Connection closed.
Best Practice: Use Virtual Environments
Always isolate your project dependencies in a virtual environment to avoid conflicts with system packages:
python -m venv myenv
source myenv/bin/activate # On Windows: myenv\Scripts\activate
pip install psycopg2 # or psycopg2-binary
Quick Reference Table
| Operating System | Install Dev Libraries | Skip Compilation |
|---|---|---|
| Ubuntu / Debian | sudo apt-get install libpq-dev python3-dev build-essential | pip install psycopg2-binary |
| RHEL / CentOS | sudo yum install postgresql-devel python3-devel gcc | pip install psycopg2-binary |
| Fedora | sudo dnf install postgresql-devel python3-devel gcc | pip install psycopg2-binary |
| macOS (Homebrew) | brew install postgresql | pip install psycopg2-binary |
| Windows | Install PostgreSQL with CLI tools, add bin to PATH | pip install psycopg2-binary |
Conclusion
The "pg_config executable not found" error in Python occurs because the psycopg2 build process needs the pg_config utility to locate PostgreSQL headers and libraries. The fix depends on your operating system but always comes down to one of two approaches:
- Install the PostgreSQL development package (
libpq-dev,postgresql-devel, orbrew install postgresql) so thatpg_configis available for source compilation. - Use
psycopg2-binaryto skip compilation entirely with a self-contained, precompiled package.
For most development workflows, pip install psycopg2-binary is the fastest path forward. For production deployments where you want your application to use system-managed PostgreSQL libraries, install the proper development headers and compile from source.