1. 程式人生 > >03-redhat-6.5升級python2.6到python2.7&安裝psycopg2&連線postgresql-9.6.1(201-02-07)

03-redhat-6.5升級python2.6到python2.7&安裝psycopg2&連線postgresql-9.6.1(201-02-07)

1、參考文件

http://ruter.sundaystart.net/2015/12/03/Update-python/

2、安裝依賴包

[root@pg96 ~]# yum -y update
[root@pg96 ~]# yum install epel-release
[root@pg96 ~]# yum install sqlite-devel
[root@pg96 ~]# yum install -y zlib-devel.x86_64 [root@pg96 ~]# yum install -y openssl-devel.x86_64

3、升級python

[root@pg96 soft]# wget http://www.python.org/ftp/python/2.7.10/Python-2.7.10.tar.xz
[root@pg96 soft]# unxz Python-2.7.10.tar.xz
[root@pg96 soft]# tar -xvf Python-2.7.10.tar



[root@pg96 Python-2.7
.10]# cd Python-2.7.10 [root@pg96 Python-2.7.10]# ./configure --enable-shared --enable-loadable-sqlite-extensions --with-zlib

找到#zlib zlibmodule.c -I (prefix)/includeL (exec_prefix)/lib -lz去掉註釋並儲存,然後進行編譯和安裝

[root@pg96 Python-2.7.10]# vim ./Modules/Setup
zlib zlibmodule.c -I
$(prefix)/include -L$(exec_prefix)/lib -lz [root@pg96 Python-2.7.10]# gmake && gmake install

安裝好Python2.7之後我們需要先把Python2.6備份起來,然後再對yum的配置進行修改,如果不進行這一步操作的話,執行yum命令將會提示你Python的版本不對。

執行以下命令,對Python2.6進行備份,然後為Python2.7建立軟連結

[root@pg96 Python-2.7.10]# mv /usr/bin/python /usr/bin/python2.6.6
[root@pg96 Python-2.7.10]# ln -s /usr/local/bin/python2.7 /usr/bin/python

然後編輯/usr/bin/yum,將第一行的#!/usr/bin/python修改成#!/usr/bin/python2.6.6

[root@pg96 Python-2.7.10]# vim /usr/bin/yum
[root@pg96 Python-2.7.10]# vim /etc/ld.so.conf

這裡寫圖片描述


[root@pg96 Python-2.7.10]# /sbin/ldconfig

[root@pg96 Python-2.7.10]# /sbin/ldconfig -v

[root@pg96 Python-2.7.10]# python
Python 2.7.10 (default, Feb  7 2017, 09:53:17)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

4、安裝pip

[root@pg96 soft]# wget https://bootstrap.pypa.io/get-pip.py
[root@pg96 soft]# python get-pip.py

找到pip2.7的路徑,為其建立軟鏈作為系統預設的啟動版本

[root@pg96 soft]# whereis pip
pip: /usr/local/bin/pip /usr/local/bin/pip2.7
[root@pg96 soft]#
[root@pg96 soft]# ln -s /usr/local/bin/pip2.7 /usr/bin/pip

5、安裝psycopg2

[root@pg96 ~]# pip install psycopg2
Collecting psycopg2
Installing collected packages: psycopg2
Successfully installed psycopg2-2.6.

6、連線,查詢,插入測試

[root@pg96 soft]# cat test_create.py
# -*- coding: UTF-8 -*-

import os,sys
import psycopg2
import time
import psycopg2.extras
print "Hello world"


conn = psycopg2.connect(host='192.168.181.141', port=5432, user='postgres', password='postgres', database='test01')

print "Opened database successfully"
cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute('''CREATE TABLE COMPANY
       (ID INT PRIMARY KEY     NOT NULL,
       NAME           TEXT    NOT NULL,
       AGE            INT     NOT NULL,
       ADDRESS        CHAR(50),
       SALARY         REAL);''')
print "Table created successfully"


conn.commit()
cur.close()
conn.close()

建表結果:

[root@pg96 soft]# python test_create.py
Hello world
Opened database successfully
Table created successfully

這裡寫圖片描述

測試插入:

[root@pg96 soft]# cat test_insert.py
# -*- coding: UTF-8 -*-

import os,sys
import psycopg2
import time
import psycopg2.extras
print "Hello world"

conn = psycopg2.connect(host='192.168.181.141', port=5432, user='postgres', password='postgres', database='test01')

print "Opened database successfully"
cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute('''insert into COMPANY values
       (1,'tom',20,'china',10000);''')
print "Table inserted successfully"

conn.commit()
cur.close()
conn.close()
[root@pg96 soft]#

插入結果:
這裡寫圖片描述

查詢測試:

[root@pg96 soft]# cat test_select.py
# -*- coding: UTF-8 -*-

import os,sys
import psycopg2
import time
import psycopg2.extras
print "Hello world"


conn = psycopg2.connect(host='192.168.181.141', port=5432, user='postgres', password='postgres', database='test01')

print "Opened database successfully"
cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute('''select * from company;''')
print "Table selected successfully"
result = cur.fetchall()
print(result)


conn.commit()
cur.close()
conn.close()

查詢結果:

[[email protected] soft]# python test_select.py
Hello world
Opened database successfully
Table selected successfully
[[1, 'tom', 20, 'china                                             ', 10000.0]]