1

I have searched the web for a few hours it turns out nothing works.

I was trying to use PostgreSQL on a Django project and I am using Python3.4 on CentOS 6.5 server.

However I am unable to install psycopg2 library to get PostgreSQL to work. Almost all the articles I found pointed me to use apt-get on CentOS but apt-get is not a standard tool on CentOS 6.5. I failed to install apt-get so I was unable to install psycopg2. Is there anything else I can do to install psycopg2 with Python 3.4?

newguy
  • 4,988
  • 10
  • 47
  • 84
  • possible duplicate of [How to install psycopg2 with "pip" on Python?](http://stackoverflow.com/questions/5420789/how-to-install-psycopg2-with-pip-on-python) – newguy Dec 07 '14 at 17:36
  • 1
    After I read some of the less voted answers in http://stackoverflow.com/questions/5420789/how-to-install-psycopg2-with-pip-on-python?rq=1 I found the answer to my problem. I have to add `/usr/pgsql-9.3/bin` to the $PATH before export $PATH – newguy Dec 07 '14 at 17:38

2 Answers2

4

Use following steps:

1. yum install postgresql-server
2. yum install python-devel
3. service postgresql initdb
4. service postgresql start
5. yum install python-psycopg2
Tejas
  • 171
  • 1
  • 9
0

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.

  • On reflection, it might be better if the default 'pip' was the python3 version. I think Centos can be trusted to maintain the python that it uses (python2) via yum. My (only slightly informed) sense is that the majority of active python development uses version three of python, and people who use python tools like pip, virtualenv and others are more likely to be using python3. Again, being very new to python I don't know which version makes the best default for application developers; opinions welcome. – Clovis_Sangrail Oct 18 '16 at 15:05