“pg_config executable not found” [Resolved]
As a software engineer, you may often find yourself installing and configuring various packages to ensure your development environment is set up correctly.
One common issue that arises when working with PostgreSQL in Python is the “pg_config executable not found” error.
This error typically occurs when the necessary PostgreSQL development packages are not installed on your system.
Here’s a step-by-step guide to resolve this issue efficiently.
Error Message Breakdown
When you encounter the following error message, it indicates that the pg_config
executable, which is essential for building psycopg2
from source, is missing:
...
× python setup.py egg_info did not run successfully.
│ exit code: 1
╰─> [23 lines of output]
running egg_info
creating /tmp/pip-pip-egg-info-i6ik3jq0/psycopg2.egg-info
writing /tmp/pip-pip-egg-info-i6ik3jq0/psycopg2.egg-info/PKG-INFO
writing dependency_links to /tmp/pip-pip-egg-info-i6ik3jq0/psycopg2.egg-info/dependency_links.txt
writing top-level names to /tmp/pip-pip-egg-info-i6ik3jq0/psycopg2.egg-info/top_level.txt
writing manifest file '/tmp/pip-pip-egg-info-i6ik3jq0/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 ...
Steps to Fix the Error
- Update Your Package List First, ensure your package list is up to date by running the following command:
sudo apt-get update
- Install PostgreSQL Development Packages Install the necessary development packages for PostgreSQL, which include
pg_config
:
sudo apt-get install libpq-dev
- Add pg_config to Your PATH You need to ensure that the
pg_config
executable is included in your system’s PATH. Run the following command to add it:
export PATH=$PATH:/usr/bin/pg_config
- Reload Your Shell Configuration Apply the changes to your current shell session by reloading your shell configuration:
source ~/.bashrc
- Install psycopg2 With the necessary dependencies installed and your PATH updated, you can now install the
psycopg2
library:
pip install psycopg2
- Verify the Installation Finally, verify that
psycopg2
is installed correctly by opening an interactive Python shell and checking the version:
python -i
In the interactive shell, run:
import psycopg2
print(psycopg2.__version__)
This should display the version number of the installed psycopg2
library, confirming that the installation was successful.
Now, feel free to continue with whatever you were trying to do before this error attempted to frustrate your day.
And you are welcome :D.