23

I'm trying to install psycopg2 on CentOS, I followed everything on this tutorial from "On with it: Installing Python 2.6" all the way to when it imports psycopg2, but when I try to import I get the following error:

 Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/python2.6/lib/python2.6/site-packages/psycopg2/__init__.py", line 69, in <module>
    from _psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
ImportError: libpq.so.5: cannot open shared object file: No such file or directory

How to troubleshoot this?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
hdx
  • 4,198
  • 7
  • 26
  • 33

5 Answers5

25

psycopg2 is a python wrapper around the PostgreSQL libraries, so you need those installed on your system too.

Since you're using CentOS, try this from the command line to install the postgre libs.

yum install postgresql-libs

wkl
  • 77,184
  • 16
  • 165
  • 176
23
  1. Forget your tutorial.
  2. Install EPEL
  3. yum install python-psycopg2

Django supports Python 2.4 just fine. If you really need Python 2.6 you can also use EPEL (yum install python26) - but there's no psycopg2 package for it yet.

Tometzky
  • 22,573
  • 5
  • 59
  • 73
4

I followed the first answer to install python libs:

yum install postgresql-lib

but it didn't work, so I also did a yum install of the devel and python:

yum install postgresql91-devel.x86_64
yum install postgresql91-python.x86_64

Not sure which one did it, but my guess is the devel.

Mxyk
  • 10,678
  • 16
  • 57
  • 76
Coaden
  • 466
  • 1
  • 4
  • 11
0

I had the same issue that python-psycopg2 disappeared after a PostgreSQL uninstallation and yum install python-psycopg2 no longer found the package. Here is how I got it installed again.

First, install the pgdg-redhat yum repository:

yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-6-x86_64/pgdg-redhat-repo-latest.noarch.rpm

This will create the file /etc/yum.repos.d/pgdg-redhat-all.repo which lists many repositories (of which most seem to be broken). The only source of python-psycopg2 seems to be the pgdg11 repo:

[pgdg11]
name=PostgreSQL 11 for RHEL/CentOS $releasever - $basearch
enabled=0
baseurl=https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-$releasever-$basearch
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG

Now you can install the package by explicitly enabling the repository containing the package:

yum install python-psycopg2 --enablerepo=pgdg11
dokaspar
  • 8,186
  • 14
  • 70
  • 98
-4

for ubuntu you'll want

sudo apt-get install python2.7-psycopg2

or if using python 3

sudo apt-get install python3-psycopg2

Alvin
  • 2,533
  • 33
  • 45
  • not sure why this got a few down-votes -- was up-voted previously for being useful to other ubuntu users – Alvin Aug 28 '14 at 00:10
  • likely because plpython is not psycopg2, they are very different things – eseglem Oct 17 '14 at 16:07
  • 3
    also: question is about CentOS, not about Ubuntu. – GwynBleidD May 04 '15 at 10:26
  • currently at -4 -- originally found this through google searching about ubuntu so figured I'd help the next guy -- 1 more comment asking to remove and it's gone... or ask to keep it – Alvin Mar 30 '16 at 03:53