I just dealt with this issue on Centos 7.2, perhaps my results will be
applicable to your situation. I'm a SysAdmin for about 40 Centos machines, but
I am very, very new to python.
When updating Centos software my instinct is to try to do as much as possible
via system utilities like yum, and within yum use the standard repositories as much as possible. My experience is that installing from other sources, and/or compiling and installing from source, can lead to version/compatibility issues. And when you want to use python3, you must be careful not to break the version two python that much of Centos (yum, for example) requires.
But in this case, I was not able to get psycopg2 to run properly under both
the system python (python 2.7) and python3.4 via yum. I had to use the python 'pip' install utility, and I first had to install pip itself (pip3, to be specific) outside of yum as well.
I have the following yum repositories:
# yum repolist
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirror.symnds.com
* epel: mirror.symnds.com
* extras: centos.mirror.nac.net
* updates: centos.mirror.nac.net
repo id repo name status
!base/7/x86_64 CentOS-7 - Base 9,007
!epel/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 10,681
!extras/7/x86_64 CentOS-7 - Extras 392
!updates/7/x86_64 CentOS-7 - Updates 2,529
repolist: 22,609
#
I installed the following python3 packages:
# yum list installed | grep python3
python3-rpm-macros.noarch 3-10.el7 @epel
python34.x86_64 3.4.3-7.el7 @epel
python34-debug.x86_64 3.4.3-7.el7 @epel
python34-devel.x86_64 3.4.3-7.el7 @epel
python34-libs.x86_64 3.4.3-7.el7 @epel
python34-setuptools.noarch 19.2-3.el7 @epel
python34-setuptools_scm.noarch 1.10.1-2.el7 @epel
python34-test.x86_64 3.4.3-7.el7 @epel
python34-tkinter.x86_64 3.4.3-7.el7 @epel
python34-tools.x86_64 3.4.3-7.el7 @epel
#
The list of regular python packages is much longer (including 'python-' versions
of all the above python3 packages). It includes three psycopg2 packages:
# yum list installed | grep psycopg2
python-psycopg2.x86_64 2.5.1-3.el7 @base
python-psycopg2-debug.x86_64 2.5.1-3.el7 @base
python-psycopg2-doc.x86_64 2.5.1-3.el7 @base
#
There was no package named python3-psycopg2. After doing this, I was able to
open the psycopg2 package inside a python program, but not in any python34 program. There I would always get an error message telling me that there was no psycopg2 module. The first of these two test programs would work, but not the 2nd:
$ cat tpy1.py
#!/usr/bin/python
import sys
print sys.path
import psycopg2
try:
conn = psycopg2.connect("dbname='dev' user='i2' host='localhost' password='pp'" )
except:
print( "Cannot connect to dev database on localhost" )
conn.close()
$ cat tpy1.py3
#!/usr/bin/python3
import sys
print( sys.path )
import psycopg2
try:
conn = psycopg2.connect("dbname='dev' user='i2' host='localhost' password='pp'" )
except:
print( "Cannot connect to dev database on localhost" )
conn.close()
$
To make psycopg2 accessible to python34, I had to first install the pip3 program, and then use it to retrieve and install psycopg2 for python34. i found helpful instructions at:
http://ask.xmodulo.com/install-python3-centos.html
I did the following as root:
# curl -O https://bootstrap.pypa.io/get-pip.py
# /usr/bin/python3.4 get-pip.py
# /usr/bin/python get-pip.py
This gave me the following versions of 'pip':
# ls -l /usr/bin/pip*
-rwxr-xr-x 1 root root 204 Oct 13 15:59 /usr/bin/pip
-rwxr-xr-x 1 root root 204 Oct 13 15:59 /usr/bin/pip2
-rwxr-xr-x 1 root root 204 Oct 13 15:59 /usr/bin/pip2.7
-rwxr-xr-x 1 root root 207 Oct 13 15:33 /usr/bin/pip3
-rwxr-xr-x 1 root root 207 Oct 13 15:33 /usr/bin/pip3.4
#
It is probably best that the default version of 'pip' is the one for the system python, python 2.7. After this I was finally able to build and install a version of psycopg2 accessible to python3, via:
# pip3 install psycopg2
Now both test programs work.