35

I need to install psycopg2 for openerp installation process, but I always faced that error. I've already used pip and easy_install and my gcc is the latest version. I really need your help to solve my problem.

This is my complete error with easy_install:

[root@server01 ~]# easy_install psycopg2    
Searching for psycopg2    
Reading http://pypi.python.org/simple/psycopg2/    
Reading http://initd.org/psycopg/    
Reading http://initd.org/projects/psycopg2    
Best match: psycopg2 2.4.5    
Downloading http://initd.org/psycopg/tarballs/PSYCOPG-2-4/psycopg2-2.4.5.tar.gz    
Processing psycopg2-2.4.5.tar.gz    
Running psycopg2-2.4.5/setup.py -q bdist_egg --dist-dir /tmp/easy_install-anWVvJ/psycopg2-2.4.5/egg-dist-tmp-cZbdtn

no previously-included directories found matching 'doc/src/_build' In file included from psycopg/psycopgmodule.c:27:
./psycopg/psycopg.h:31:22: error: libpq-fe.h: No such file or directory In file included from psycopg/psycopgmodule.c:29:

...

error: Setup script exited with error: command 'gcc' failed with exit status 1
Salek
  • 449
  • 1
  • 10
  • 19
ws_123
  • 5,155
  • 7
  • 23
  • 20

13 Answers13

48

You'll need to install the development package for PostgreSQL, which for instance under Ubuntu is a sudo apt-get install libpq-dev... and for CentOS it's yum install postgresql-devel

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
34

For the case of CentOS, I had this very same problem, that I solved installing these packages:

sudo yum install postgresql-libs
sudo yum install postgresql-devel
sudo yum install python-devel

only then, I was able to perform successfully:

sudo easy_install psycopg2
hafichuk
  • 10,351
  • 10
  • 38
  • 53
Nicolás Ozimica
  • 9,481
  • 5
  • 38
  • 51
14

May Your system is missing the Python headers. To fix this, run:

sudo apt-get install python-dev

or

sudo apt-get install python3-dev
Nicolás Ozimica
  • 9,481
  • 5
  • 38
  • 51
Heroic
  • 980
  • 4
  • 12
  • 1
    I searched 30 min. Installed python-psycopg2, postgresql-server-dev-9.1, postgresql-plpython-9.1 and libpq-dev. Still got '"gcc" failed with exit status 1. Now with python-dev it works fine ;-) – surfi Aug 20 '13 at 15:14
  • 2
    unless you're using CentOS – hafichuk Mar 05 '14 at 21:28
  • opensuse users should use: sudo zypper install python3-dev – Masih Nov 14 '14 at 13:20
14

On a fresh ubuntu 14.04.2 LTS on an EC2 instance, I was able to install psycopg2 after instaling the following packages:

sudo apt-get install gcc
sudo apt-get install postgres-xc-server-dev
sudo apt-get install libpq-dev
sudo apt-get install postgresql-client-common
sudo apt-get install postgresql-common

And on a fresh AMI linux (CentOS) instance:

sudo yum install postgresql-devel
sudo yum install gcc*

Hope it helps,

Erez

Prashant Gaur
  • 9,540
  • 10
  • 49
  • 71
ErezK
  • 542
  • 1
  • 5
  • 10
10

i had this problem but was solved by running:

pip install -Iv psycopg2==2.7.7

There is a problem installing the latest version + python3.7

Joe Gasewicz
  • 1,252
  • 15
  • 20
6

I've been trying to install pgsql on a plain Amazon Linux EC2 instance, and have been getting this error. Per Nicolás answer, I installed his suggested list of packages, but I continued to get the error. Looking more closely at the error, I noticed that the gcc command was including pgsql93 files:

gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -DPSYCOPG_DEFAULT_PYDATETIME=1 -DPSYCOPG_VERSION="2.6.1 (dt dec pq3 ext lo64)" -DPG_VERSION_HEX=0x09030A -DHAVE_LO64=1 -I/usr/include/python2.7 -I. -I/usr/include/pgsql93 -I/usr/include/pgsql93/server -c psycopg/psycopgmodule.c -o build/temp.linux-x86_64-2.7/psycopg/psycopgmodule.o -Wdeclaration-after-statement

Looking in /usr/include/ I see pgsql92/ but not pgsql93/. So I took a look at what pgsql-related yum packages were installed and I noticed these:

postgresql92.x86_64 postgresql92-devel.x86_64 postgresql92-libs.x86_64 postgresql93.x86_64 postgresql93-libs.x86_64

Seemed to me like there ought to be a postgresql93-devel, so I tried:

sudo yum install postgresql93-devel

And that fixed the problem for me! So I guess the theme is: Look at the gcc call, and see what dir it's pulling in. Follow the crumbs from there.

Aaron Torgerson
  • 992
  • 1
  • 7
  • 12
5

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.

neciu
  • 4,373
  • 2
  • 24
  • 33
  • 1
    Man I tried everything before this finally fixed it. Also running a docker image, so pretty much just copied your code into my dockerfile and it worked like a charm – Simon May 20 '16 at 16:20
3

In centos 7. i'd use sudo yum install postgresql96-devel.x86_64 to solved my problem.

ji-ruh
  • 725
  • 1
  • 7
  • 24
2

The invocation of gcc failed, gcc couldn't compile the source. Probably the cause is

./psycopg/psycopg.h:31:22: error: libpq-fe.h: No such file or directory In file included from psycopg/psycopgmodule.c:29:

that the header libpq-fe.h is nowhere in the search path if you have it at all.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
  • what should i do with libpq-fe.h? could you give me more explanation? – ws_123 Oct 16 '12 at 09:59
  • Check if you have it at all. If not, install the package that provides it, `libpq-devel` or `libpq-dev` or something similar, depending on distribution. – Daniel Fischer Oct 16 '12 at 10:02
2

This worked for me on Ubuntu Server 14.10 64-bit:

sudo apt-get install libpqxx-dev
schnbrg
  • 21
  • 2
1

For me, the missing package was

libpqxx-devel.x86_64
Jeff
  • 4,285
  • 15
  • 63
  • 115
1

psycopg2 is also available on PyPI in the form of wheel packages for the most common platform (Linux, OSX, Windows): this should make you able to install a binary version of the module, not requiring the above build or runtime prerequisites.

You can use this command : pip install psycopg2-binary

Note The psycopg2-binary package is meant for beginners to start playing with Python and PostgreSQL without the need to meet the build requirements. If you are the maintainer of a publish package depending on psycopg2 you shouldn’t use ‘psycopg2-binary’ as a module dependency. For production use you are advised to use the source distribution.

Dksingh
  • 61
  • 1
  • 11
  • so... why am I getting " unable to execute 'gcc': No such file or directory" when running that command? why does it need gcc if I'm trying to install the binary version of the module? – eis Dec 16 '20 at 19:13
0

I was running 3.9-alpine for CICD and had to install these packages to get it working;

apk add postgresql-dev
apk add gcc
apk add musl-dev

Then I was able to run a pip install psycopg2 as normal.

Edward Spencer
  • 448
  • 8
  • 10