Solving Error: pg_config executable not found when installing psycopg2 on Alpine in Docker

Error: pg_config executable not found when installing psycopg2 on Alpine in Docker is a Python WebDriver error which occurs when psycopg2 dependencies are not installed.

In this article I am going to try and solve the error while I try to explain why the error is actually popping up, we will also introduce some solutions that worked for other developers and see if those solutions can solve the error in your particular situation.

Explaining the Error : Error pg_config executable not found when installing psycopg2 on Alpine in Docker

First of all we need to understand why the error happens at all, the error occurs psycopg2 dependencies are not installed.

Bellow is an example of the error message.

                                                                       #
Collecting psycopg2==2.7.3.1 (from -r requirements.txt (line 9))
  Downloading psycopg2-2.7.3.1.tar.gz (425kB)
    Complete output from command python setup.py egg_info:
    running egg_info
....
    Error: pg_config executable not found.
                                                                       #

To solve the problem above, I have a couple of solutions which have worked for me, bellow is a detailed explanation of both.

Solution 1 : install psycopg2 dependencies

The first solution is to install psycopg2 dependencies, you can do that by running these commands first.

First, start by running the following command.

                                                                       #
RUN apk update
                                                                       #

Then, run this second command.

                                                                       #
RUN apk add postgresql-dev gcc python3-dev musl-dev
                                                                       #

Now you can install the other dependencies in dockerfile from requirements.txt.

If the solution above did not work try the solution bellow.

Solution 2 : install psycopg2 dependencies

The solution that will work for most people is to install the dependencies by doing this.

Open your dockerfile and add the lines bellow.

                                                                       #
RUN apt-get update \
    && apt-get -y install libpq-dev gcc \
    && pip install psycopg2
                                                                       #

This should be enough to install the dependencies.

I hope the commands above fixed your problem, good luck with the scripts to come.

Summing-up

That is it guys, we arrived at the end of this article, I did my best to help you solve this issue, I wish you good luck with your Python projects and I wish you good luck with the errors to come, Python is full of errors. But, It is our Favourite Programming Language, Cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/