Are you encountering the error “pg_config executable not found” while building psycopg2 from source? If so, you’re not alone. This error can be frustrating and hinder your progress in using psycopg2, a popular PostgreSQL adapter for Python.
Don’t worry! This comprehensive troubleshooting guide will help you resolve the Error: pg_config Executable Not Found and successfully build psycopg2 from source. Read on to learn the step-by-step solutions and expert tips.
How to fix Error: pg_config executable not found?
To fix the “pg_config executable not found” error and proceed with building psycopg2 from source, follow the steps below:
1. Verify PostgreSQL Installation
Before diving into the troubleshooting process, it’s crucial to ensure that PostgreSQL is properly installed on your system. Here’s how you can check:
- Open your command prompt or terminal.
- Enter the command
psql --version
and press Enter. - If PostgreSQL is installed, the command will display the version number. Otherwise, you need to install PostgreSQL.
2. Install PostgreSQL
If PostgreSQL is not installed on your system, follow these instructions to install it:
- Visit the official PostgreSQL website at https://www.postgresql.org.
- Navigate to the Downloads section and select the appropriate version for your operating system.
- Download the installer and run it.
- Follow the on-screen instructions to complete the installation process.
Once PostgreSQL is successfully installed, proceed to the next step.
3. Locate the pg_config Executable
The “pg_config executable not found” error occurs when the build process cannot locate the pg_config
executable. This executable is essential for building psycopg2 from source. Follow the steps below to locate it:
- Open a file explorer or terminal.
- Navigate to the PostgreSQL installation directory.
- On Windows, the default installation directory is usually
C:\Program Files\PostgreSQL
. - On macOS, it is commonly found at
/Library/PostgreSQL
. - On Linux, the installation directory can vary depending on the distribution.
- On Windows, the default installation directory is usually
- Once you’re in the PostgreSQL installation directory, locate the
bin
folder. - Look for the file named
pg_config
within thebin
folder. This is thepg_config
executable.
4. Add pg_config to the System Path
To ensure the pg_config
executable is recognized globally, you need to add it to the system’s PATH environment variable. Follow these steps:
Windows
- Open the Control Panel and navigate to System or System and Security.
- Click on System.
- On the left sidebar, click on Advanced system settings.
- In the System Properties window, click on the Environment Variables button.
- In the System variables section, scroll down and find the Path variable.
- Select the Path variable and click on Edit.
- Click on New and enter the path to the
pg_config
executable. For example,C:\Program Files\PostgreSQL\bin
. - Click OK to save the changes.
- Restart your computer to apply the updated PATH variable.
macOS and Linux
- Open a terminal window.
- Enter the following command to open the shell profile configuration file:
nano ~/.bash_profile
If you’re using a different shell (e.g., zsh), replace ~/.bash_profile with the corresponding configuration file (e.g., ~/.zshrc).
- Add the following line to the file, replacing
/path/to/postgresql/bin
with the actual path to thebin
directory containingpg_config
:
export PATH=”/path/to/postgresql/bin:$PATH”
- Press Ctrl+X, then Y, and finally Enter to save the changes.
- Restart your terminal or run
source ~/.bash_profile
(or the appropriate command for your shell) to apply the changes.
5. Verify pg_config Availability
After adding pg_config
to the system’s PATH variable, you should verify its availability. Open a new terminal or command prompt and enter the following command:
pg_config –version
If the pg_config
executable is correctly recognized, it will display the version number. If not, revisit the previous steps and ensure you followed them accurately.
6. Build psycopg2 from Source
Now that the pg_config
executable is properly set up, you can proceed with building psycopg2 from source. Follow these steps:
- Download the source code for psycopg2 from the official GitHub repository (https://github.com/psycopg/psycopg2).
- Extract the downloaded source code to a convenient location on your system.
- Open a terminal or command prompt and navigate to the extracted source code directory.
- Run the following command to build psycopg2:
python setup.py build_ext –pg-config /path/to/pg_config build
- Replace /figpath/to/pg_con with the actual path to the pg_config executable. For example, /usr/local/pgsql/bin/pg_config.
- Once the build process completes successfully, you can proceed with installing psycopg2:
python setup.py install
- After installation, you should be able to import and use psycopg2 in your Python projects without encountering the “pg_config executable not found” error.
Conclusion
Building psycopg2 from source is a common task for Python developers working with PostgreSQL databases. The “pg_config executable not found” error can be resolved by following the step-by-step troubleshooting process outlined in this guide.
Remember to ensure that PostgreSQL is correctly installed, locate the pg_config
executable, add it to the system’s PATH variable, and verify its availability. Once you’ve successfully built and installed psycopg2, you can harness its power to interact with PostgreSQL databases seamlessly.
Now you have the knowledge and tools to overcome the “pg_config executable not found” error and continue your Python and PostgreSQL endeavors without obstacles.