Error: pg_config executable not found. (psycopg2 for postgres in docker not successfully installed)

3,394 views
Skip to first unread message

Adeyemi Deji

unread,
Oct 28, 2022, 6:35:07 AM10/28/22
to Django users
Hello Fam!

I'm currently going through djangoforprofessional tutorial and I've been trying to resolve an error related to installing psycopg2 for postgres in a docker container.

After setting up Dockerfile, requirements.txt, and docker-compose.yml which is shown below, I entered this command: docker-compose up -d --build
and the returned output (error) is shown below.

I have tried this command in Dockerfile: RUN pip install --user psycopg2==2.9.5 
I got the command on StackOverflow but still didn't resolve the issue.

Thank you very much in advance.

Error Output
#0 23.39 Collecting psycopg2==2.9.5
#0 23.55   Downloading psycopg2-2.9.5.tar.gz (384 kB)
#0 25.11      ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 384.3/384.3 kB 246.3 kB/s eta 0:00:00
#0 25.20   Preparing metadata (setup.py): started
#0 26.74   Preparing metadata (setup.py): finished with status 'error'
#0 26.78   error: subprocess-exited-with-error
#0 26.78
#0 26.78   × python setup.py egg_info did not run successfully.
#0 26.78   │ exit code: 1
#0 26.78   ╰─> [25 lines of output]
#0 26.78       /usr/local/lib/python3.12/site-packages/setuptools/config/setupcfg.py:508: SetuptoolsDeprecationWarning: The license_file parameter is deprecated, use license_files instead.
#0 26.78         warnings.warn(msg, warning_class)
#0 26.78       running egg_info
#0 26.78       creating /tmp/pip-pip-egg-info-g5ko86oj/psycopg2.egg-info
#0 26.78       writing /tmp/pip-pip-egg-info-g5ko86oj/psycopg2.egg-info/PKG-INFO
#0 26.78       writing dependency_links to /tmp/pip-pip-egg-info-g5ko86oj/psycopg2.egg-info/dependency_links.txt
#0 26.78       writing top-level names to /tmp/pip-pip-egg-info-g5ko86oj/psycopg2.egg-info/top_level.txt
#0 26.78       writing manifest file '/tmp/pip-pip-egg-info-g5ko86oj/psycopg2.egg-info/SOURCES.txt'
#0 26.78
#0 26.78       Error: pg_config executable not found.
#0 26.78
#0 26.78       pg_config is required to build psycopg2 from source.  Please add the directory
#0 26.78       containing pg_config to the $PATH or specify the full executable path with the
#0 26.78       option:
#0 26.78
#0 26.78           python setup.py build_ext --pg-config /path/to/pg_config build ...
#0 26.78
#0 26.78       or with the pg_config option in 'setup.cfg'.
#0 26.78
#0 26.78       If you prefer to avoid building psycopg2 from source, please install the PyPI
#0 26.78       'psycopg2-binary' package instead.
#0 26.78
#0 26.78       For further information please check the 'doc/src/install.rst' file (also at
#0 26.78       <https://www.psycopg.org/docs/install.html>).
#0 26.78
#0 26.78       [end of output]
#0 26.78
#0 26.78   note: This error originates from a subprocess, and is likely not a problem with pip.
#0 26.78 error: metadata-generation-failed
#0 26.78
#0 26.78 × Encountered error while generating package metadata.
#0 26.78 ╰─> See above for output.
#0 26.78
#0 26.78 note: This is an issue with the package mentioned above, not pip.
#0 26.78 hint: See above for details.
------
failed to solve: executor failed running [/bin/sh -c pip install -r requirements.txt]: exit code: 1

requirements.txt file
asgiref==3.5.2
Django==4.1.2
psycopg2==2.9.5
sqlparse==0.4.3
tzdata==2022.5

Dockerfile
# Pull Base Image

FROM python:3.12.0a1-slim-bullseye

# Set Environment Variables

ENV PIP_DISABLE_PIP_VERSION_CHECK 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set Work Directory

WORKDIR /code

# Install Dependencies

COPY ./requirements.txt .
RUN pip install -r requirements.txt

# Copy Project

COPY . .

docker-compose.yml
version: '3.9'
services:
  web:
    build: .
    command: python /code/manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db
  db:
    image: postgres:13
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    environment:
      - "POSTGRES_HOST_AUTH_METHOD=trust"
volumes:
  postgres_data:



C:\Users\adede\wdev\djangoP\django-docker-postgre>

Kasper Laudrup

unread,
Oct 29, 2022, 8:12:22 AM10/29/22
to django...@googlegroups.com
On 28/10/2022 12.35, Adeyemi Deji wrote:
>
> I have tried this command in Dockerfile: RUN pip install --user
> psycopg2==2.9.5
> I got the command on StackOverflow but still didn't resolve the issue.
>

Seems like you need the postgresql development package or similar to
build psycopg2 from source. Try using the binary package
(psycopg2-binary) instead as suggested here:

> #0 26.78       If you prefer to avoid building psycopg2 from source,
> please install the PyPI
> #0 26.78       'psycopg2-binary' package instead.

Alternatively, ensure the postgresql development package is installed in
your Docker container.

Kind regards,
Kasper Laudrup
OpenPGP_0xE5D9CAC64AAA55EB.asc
OpenPGP_signature

Adeyemi Deji

unread,
Oct 31, 2022, 2:32:42 PM10/31/22
to Django users

Hello, fam!

Found a solution to the problem. Updated the Dockerfile. Check below

Thanks, guys...

Dockerfile

FROM python:3.8.3-slim

ENV PIP_DISABLE_PIP_VERSION_CHECK=1
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

WORKDIR /code

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

COPY ./requirements.txt .
RUN pip install -r requirements.txt

COPY . .

Kasper Laudrup

unread,
Oct 31, 2022, 2:51:49 PM10/31/22
to django...@googlegroups.com
On 31/10/2022 19.32, Adeyemi Deji wrote:
>
> Hello, fam!
>
> Found a solution to the problem. Updated the Dockerfile. Check below
>

Great to hear you figured it out and thanks for sharing. Out of
curiosity, any reason why you chose to build the psycopg2 package from
source instead of using a prebuilt binary?

I would assume using the prebuilt binary would have been simpler and of
course make it much faster to build your container than building your
own package from source.

Thanks and kind regards,
Kasper Laudrup
OpenPGP_0xE5D9CAC64AAA55EB.asc
OpenPGP_signature

Adeyemi Deji

unread,
Nov 1, 2022, 6:30:53 AM11/1/22
to django...@googlegroups.com
Hi, 

I believe the the binary package is suitable for development and I plan integrating docker in my company's upcoming project, so I needed a package best for production.

Thanks kasper 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/fa5906c5-929f-a2b8-91e2-72d8b0fb8194%40stacktrace.dk.

subin

unread,
Nov 3, 2022, 4:16:30 PM11/3/22
to django...@googlegroups.com
Hi there! We just wanted to let you know we received your message and will be in touch before our offices close at

subin

unread,
Nov 3, 2022, 4:42:37 PM11/3/22
to django...@googlegroups.com
We have received your message and will be in touch within

subin

unread,
Nov 3, 2022, 4:44:32 PM11/3/22
to django...@googlegroups.com
Please let me know if that is acceptable with you.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Adeyemi Deji

unread,
Nov 3, 2022, 5:11:22 PM11/3/22
to django...@googlegroups.com
Also, i tried psycopg2-binary ànd still add the same error issue but when i updated the Dokerfile, things got normal. 

On Mon, 31 Oct 2022, 19:51 'Kasper Laudrup' via Django users, <django...@googlegroups.com> wrote:
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages