How to install psycopg2 on Linux properly

psycopg2 is a useful python package for handling PostgreSQL. However, you may find something wrong like pg_config executable not found. when installing this package. In this post, I will teach you how to install it successfully.

How to install python3 and pip3 on Linux

Though it is basic, I still would like to review it with you.

For Debian

1
2
3
apt update
apt upgrade
apt install python3 python3-pip python-is-python3

For Alpine Linux

1
apk add python3 py3-pip

How to install psycopg2

For Debian

1
2
3
apt install libpq-dev
apt install python3.11-dev # if you are use python3.11, you may need to run this
pip insall psycopg2

For Alpine Linux

1
2
apk add postgresql-dev gcc python3-dev musl-dev
pip install psycopg2

Reference

  1. https://stackoverflow.com/questions/11618898/pg-config-executable-not-found#:~:text=postgresql%2Ddevel%20(-,libpq%2Ddev,-in%20Debian/Ubuntu

  2. https://github.com/psycopg/psycopg2/issues/684#:~:text=alpine%0ARUN%20apk%20update%20%26%26-,apk%20add%20postgresql%2Ddev%20gcc%20python3%2Ddev%20musl%2Ddev,-pip%20install%20psycopg2

  3. https://stackoverflow.com/questions/74727501/error-could-not-build-wheels-for-psycopg2-which-is-required-to-install-pyproje#:~:text=apt%20install%20python3.11%2Ddev%20libpq%2Ddev