Error trying to install Postgres for python (psycopg2)

In my case (ubuntu 12.04 with python 2.7 and python 3.4 installed)

sudo apt-get install python3-dev 

was not enough. What did the trick was

sudo apt-get install python3.4-dev

I tried all the solution but only works be

pip install psycopg2-binary

after installing lib you can use import psycopg2 in python file and then further db connection config


For Ubuntu 14.04, from Docker image python:3.4.3-slim this combination worked for me:

sudo apt-get update
sudo apt-get install -y build-essential
sudo apt-get install -y python3.4-dev
sudo apt-get install -y libpq-dev

pip3 install psycopg2

Note build-essential package. It was crucial in my case.


The python-dev package is required for compilation of Python extensions written in C or C++, like psycopg2. If you're running a Debian-based distribution (e.g. Ubuntu), you can install python-dev by running

sudo apt install python-dev

or

sudo apt install python3-dev

depending on your python version (for example, sudo apt install python3.8-dev).

After that, proceed to install psycopg2 in your virtualenv environment as usual.