How to Fix Error: pg_config Executable Not Found?

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:

  1. Open your command prompt or terminal.
  2. Enter the command psql --version and press Enter.
  3. 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:

  1. Visit the official PostgreSQL website at https://www.postgresql.org.
  2. Navigate to the Downloads section and select the appropriate version for your operating system.
  3. Download the installer and run it.
  4. 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:

  1. Open a file explorer or terminal.
  2. 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.
  3. Once you’re in the PostgreSQL installation directory, locate the bin folder.
  4. Look for the file named pg_config within the bin folder. This is the pg_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

  1. Open the Control Panel and navigate to System or System and Security.
  2. Click on System.
  3. On the left sidebar, click on Advanced system settings.
  4. In the System Properties window, click on the Environment Variables button.
  5. In the System variables section, scroll down and find the Path variable.
  6. Select the Path variable and click on Edit.
  7. Click on New and enter the path to the pg_config executable. For example, C:\Program Files\PostgreSQL\bin.
  8. Click OK to save the changes.
  9. Restart your computer to apply the updated PATH variable.

macOS and Linux

  1. Open a terminal window.
  2. 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).

  3. Add the following line to the file, replacing /path/to/postgresql/bin with the actual path to the bin directory containing pg_config:

    export PATH=”/path/to/postgresql/bin:$PATH”

  4. Press Ctrl+X, then Y, and finally Enter to save the changes.
  5. 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:

  1. Download the source code for psycopg2 from the official GitHub repository (https://github.com/psycopg/psycopg2).
  2. Extract the downloaded source code to a convenient location on your system.
  3. Open a terminal or command prompt and navigate to the extracted source code directory.
  4. Run the following command to build psycopg2:

    python setup.py build_ext –pg-config /path/to/pg_config build

  5. Replace /figpath/to/pg_con with the actual path to the pg_config executable. For example, /usr/local/pgsql/bin/pg_config.
  6. Once the build process completes successfully, you can proceed with installing psycopg2:

    python setup.py install

     

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