03-redhat-6.5升级python2.6到python2.7&安装psycopg2&连接postgresql-9.6.1(201-02-07)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_29622761/article/details/54917316

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()

查询结果:

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

猜你喜欢

转载自blog.csdn.net/qq_29622761/article/details/54917316