python-alpine and Postgres issues
Ever ended up with this issue ?
Error: pg_config executable not found. pg_config is required to build psycopg2 from source. Please add the directory containing pg_config to the $PATH or specify the full executable path with the option: python setup.py build_ext --pg-config /path/to/pg_config build ... or with the pg_config option in 'setup.cfg'. If you prefer to avoid building psycopg2 from source, please install the PyPI 'psycopg2-binary' package instead. For further information please check the 'doc/src/install.rst' file (also at <https://www.psycopg.org/docs/install.html>).s
python:alpine is the best Docker base image to build containers for light-weight Python based applications.
Most of the python applications like Django or Flask which has Postgres database backend will be using psycopg2 as a driver for making db connections. This module works well when used on plain python Docker image, which is based on Debian.
When it comes to python:alpine, it’s a light weight stripped down image with bare minimum dependencies. Some of the dev dependencies like gcc compilers are not part of alpine image. This causes issue when we try to install psycopg2 with pip.
We just need to install certain dev dependencies to solve this issue. Add the below line before you install psycopg2 or psycopg2-binary with pip.
RUN apk update && apk add postgresql-dev gcc python3-dev musl-dev
This will ensure the os level dependencies are added for psycopg2.