Nishil Patel
May 3, 2024
7 min read
In this article, you’ll learn what goes behind the scenes when you get the “Error: pg_config executable not found.” error message while installing or using pyscopg2 with Python and PostgreSQL databases. You’ll also learn what causes this error and how to fix it for different operating systems.
1.
Introduction
2.
What is Psycopg2?
3.
What is pg_config?
4.
How to Fix “Pg_config Executable Not Found” Error?
5.
Quick Summary
6.
Wrapping Up
7.
FAQs
Sometimes, you might encounter the “Error: pg_config executable not found.” error while installing or using pyscopg2 with Python and PostgreSQL. The error looks something like:
Error: pg_config executable not found.
Please add the directory containing pg_config to the PATH
This typically happens when the psycopg2 library has trouble communicating with your PostgreSQL database. This article will guide you through some important concepts around the error, what causes it, and how to fix it on different operating systems.
Psycopg2 is a database adapter for PostgreSQL databases compliant with Python DB API 2.0 specifications. It facilitates secure and efficient interaction with PostgreSQL for Python and is ideal for multi-threaded Python applications. The Psycopg2 library acts as a bridge between the two, enabling Python to seamlessly communicate with PostgreSQL DB.
pg_config is a configuration utility file used during the installation of psycopg2. It retrieves information (the locations of headers and other libraries for PostgreSQL) about the installed PostgreSQL version, which psycopg2 needs to interact with the PostgreSQL client library (like libpq). This ensures compatibility between the psycopg2 library and your specific PostgreSQL installation.
The error shows up in your terminal for several reasons. Here are the common ones:
Here are the various fixes for the error:
Here’s how to install PostgreSQL development packages for different operating systems:
Also Read: How to Fix “Objects are not Valid as a React Child” Error in React
If you’re using Homebrew as your graphical package manager for macOS, open the terminal and run the command:
brew install postgresql
If you’re using MacPorts to manage your packages, you might need to specify the exact version you want to install after the “postgresql” command. Run the following in your terminal like below to install PostgreSQL v14 with MacPorts:
sudo port install postgresql14-server
Use the following command to install the libpq-dev development package for PostgreSQL and the Python development libraries on Linux.
# Replace python3-dev with pythonX-dev for your Python version (e.g., python2-dev)
sudo apt-get install libpq-dev python3-dev
Once you’re sure about the correct installation of the PostgreSQL dev package based on your OS, install the psycopg2 library with pip using the following command:
pip install psycopg2
Sometimes, you may have to update the system PATH env variable manually by adding a directory for pg_config which enables Python to locate the pg_config file correctly — that’s necessary for installing the psycopg2 library. Here’s how you can add a directory for different operating systems:
Run the following command from the Command Prompt to permanently add the specified directory to the PATH environment variable:
setx PATH "%PATH%;C:\path\to\pg_config\directory"
This command will update the PATH env variable for future Command Prompt sessions. However, it’s important to note that the changes might not take effect in the current session; a new Command Prompt window may need to be opened to reflect the update.
Add the path to the pg_config directory to the PATH env variable by modifying the ~/.bashrc or ~/.bash_profile file with the following line:
export PATH=$PATH:/path/to/pg_config/directory
This command appends the directory containing pg_config to the existing PATH variable, ensuring that the system can locate the executable.
Remember to replace /path/to/pg_config/directory or C:\path\to\pg_config\directory with the actual path where pg_config is located on your system. Here are the common path locations for pg_config based on your operating system:
C:\Program Files\PostgreSQL <pgsql_version>\bin\pg_config.exe
/usr/local/pgsql/bin/pg_config
/usr/pgsql-<pgsql_version>/bin/pg_config
Here’s a quick summary of ways to fix the “pg_config executable not found.” error in Python:
Operating System | Installation Steps |
Windows | 1. Install PostgreSQL from https://www.postgresql.org/download/ 2. Install psycopg2 with pip install psycopg2 |
macOS (Homebrew) | 1. Install PostgreSQL with brew install postgresql 2. Install psycopg2 with pip install psycopg2 |
macOS (MacPorts) | 1. Install PostgreSQL (e.g., sudo port install postgresql14-server for v14) 2. Install psycopg2 with pip install psycopg2 |
Linux (Ubuntu/Debian) | 1. Install PostgreSQL with sudo apt-get install libpq-dev python3-dev (replace python3-dev with your Python version) 2. Install psycopg2 with pip install psycopg2 |
By following the aforementioned steps, you should be able to resolve the "pg_config executable not found." error and successfully install psycopg2 for interacting with your PostgreSQL database from Python.
Nishil is a successful serial entrepreneur. He has more than a decade of experience in the software industry. He advocates for a culture of excellence in every software product.
Meet the Author: Nishil Patel, CEO, and Co-founder of BetterBugs. With a passion for innovation and a mission to improve software quality.
We never spam.
Share your experience with the founderhere!