参考文档:
postgresql下载(源码或者安装包均可):http://www.postgresql.org/download/
python下载(源码包): https://www.python.org/downloads/
psycopg下载(源码包): http://initd.org/psycopg/download/
postgresql的python接口psycopy安装文档:http://initd.org/psycopg/docs/install.html
python基础教程: http://www.runoob.com/python/python-install.html
psycopy连接postgresql基本API: http://developer.51cto.com/art/201401/426204_all.htm
操作步骤:
环境:CentOS6.3 32bit
安装包版本选择。
首先根据psycopg的文档介绍, 下载对应的postgresql和python的版本。
psycopg2目前支持的是:
Python 2 versions from 2.5 to 2.7
Python 3 versions from 3.1 to 3.4
PostgreSQL versions from 7.4 to 9.4
因此,我下载了postgresql9.3.1,python3.4.3, psycopg2-2.6.1
2.安装postgresql。
我下载的postgresql-9.3.1-linux.run安装包,正常进行图形化安装,最终将程序安装到/opt/PostgreSQL/9.3/....中。
Server [localhost]: Database [postgres]: Port [5432]: Username [postgres]: Password for user postgres: psql.bin (9.3.1) Type "help" for help. postgres=#
至此postgresql成功安装完毕。
3.安装python。
解压
tar -zxvf Python-3.4.3.tar.gz
到Python-3.4.3文件夹下:
cd Python-3.4.3
配置安装
./configure ./make ./make install
成功安装后,python会安装到/usr/local/bin中。
来到/usr/local/bin目录下:
cd /usr/local/bin
执行
[root@localhost Desktop]# python Python 2.6.6 (r266:84292, Jan 22 2014, 09:37:14) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> print "Hello, World!" Hello, World! >>>
(ctrl+d可以退出python)
至此python成功安装完毕。
4.安装psycopg.
解压源码安装包
tar -zxvf psycopg2-2.6.1.tar.gz
到psycopg2-2.6.1文件中
cd psycopg2-2.6.1
配置安装
python setup.py build_ext --pg-config /opt/PostgreSQL/9.3/bin/pg_config build
或者
export PATH=/opt/PostgreSQL/9.3/bin/:$PATH python setup.py build
过程报错: python.h no such file or directory 等等多个.h头文件不存在。
psycopg/psycopgmodule.c warning implicit declaration of function ..
psycopg/psycopgmodule.c warning implicit declaration of function
解决报错问题。需安装依赖包:
yum install python-devel
重新配置安装psycopg
python setup/py clean python setup.py build_ext --pg-config /opt/PostgreSQL/9.3/bin/pg_config build
安装过程中无报错则完成。
5.在python下使用psycopg连接postgresql:
[root@localhost Desktop]# python Python 2.6.6 (r266:84292, Jan 22 2014, 09:37:14) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import psycopg2 >>> conn = psycopg2.connect(host='192.168.100.236', port='5866', database='highgo', user='highgo', password='highgo') >>> print("connected database") connected database >>>
6. 通过文件来执行python脚本
在/root/centos32/Desktop/创建test.py脚本文件,内容如下:
print ("hello, world!"); import psycopg2; conn = psycopg2.connect(database='highgo',user='highgo',password='highgo',host='192.168.100.236', port='5866'); print "connected db"; cur=conn.cursor(); cur.execute("create table lyy(id int, name varchar)"); conn.commit(); print "create table lyy"; cur.execute("insert into lyy values(1,'lily')"); cur.execute("insert into lyy values(2,'lucy')"); conn.commit(); print("insert into lyy two rows"); cur.execute("select id, name from lyy"); rows = cur.fetchall(); for row in rows: print "id=",row[0]; print "name=",row[1]; print "operation done"; conn.close();
在Terminal终端中,执行python脚本文件:
[root@localhost Desktop]# cd /home/centos32/Desktop [root@localhost Desktop]# python test.py hello, world! connected db create table lyy insert into lyy two rows id= 1 name= lily id= 2 name= lucy operation done
测试成功.
注意:注意区别系统自带python和我们自己安装的python,
在/usr/local/bin下可以看到python3
执行python3 -V 得到3.4.3 这个是我们安装的,启动时可以直接使用。
执行python -V 得到2.x.x 这个是系统带的。