Open In App

How to Fix – “pg_config executable not found” in Python

Last Updated : 26 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

When working with databases, the psycopg2 library is commonly used to interact with PostgreSQL databases. However, while installing or working with psycopg2, you might encounter an error message that says “pg_config executable not found.” This error occurs when the required PostgreSQL configuration tool (pg_config) cannot be located during the installation process.

In this article, we will delve into the concepts behind the “pg_config executable not found” error, explore the underlying reasons for its occurrence, and provide step-by-step instructions to fix the issue and successfully install psycopg2.

Causes and Solution to Fix “pg_config executable not found” in Python

Below are the causes as well as solutions for this error in Python:

Cause 1: PostgreSQL is not Installed

One of the main reasons for this error is that PostgreSQL isn’t installed. So, ensure that PostgreSQL is installed on your system.

Solution: You can download and install PostgreSQL from the official PostgreSQL website.

Cause 2: psycopg2 not installed

It is also a reason for this error.

Solution: With the pg_config executable now accessible via the PATH environment variable, you should be able to install psycopg2 without encountering the “pg_config executable not found” error. Install psycopg2 using pip:

pip install psycopg2

Cause 3: pg_config Not Located

One of the main reasons for this error is that pg_config isn’t located.

Solution: The pg_config executable is typically included with the PostgreSQL installation. However, its location may not be included in the system’s PATH environment variable. You need to find the path to pg_config on your system. Common paths include:

On Linux: /usr/pgsql-<version>/bin/pg_config
On macOS: /usr/local/pgsql/bin/pg_config
On Windows: C:\Program Files\PostgreSQL\<version>\bin\pg_config.exe

Cause 4: PATH Environment Variable Not Updated

This problem also occur when PATH environment variable is not updated.

Solution: Add the directory containing the pg_config executable to the system’s PATH environment variable. This step allows Python to locate pg_config during the installation of psycopg2. On Linux and macOS, you can modify the ~/.bashrc or ~/.bash_profile file to include the following line:

export PATH=$PATH:/path/to/pg_config/directory

On Windows, you can modify the environment variables through the Control Panel or use the following command in the Command Prompt:

setx PATH "%PATH%;C:\path\to\pg_config\directory"

Cause 5: Missing Modules

Suppose you have PostgreSQL 13 installed on your Windows system at C:\Program Files\PostgreSQL\13\bin\pg_config.exe. You would fix the issue by executing the following commands in the Command Prompt:

setx PATH "%PATH%;C:\Program Files\PostgreSQL\13\bin"
pip install psycopg2

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads