參考文檔:html
postgresql下載(源碼或者安裝包都可):http://www.postgresql.org/download/python
python下載(源碼包): https://www.python.org/downloads/linux
psycopg下載(源碼包): http://initd.org/psycopg/download/sql
postgresql的python接口psycopy安裝文檔:http://initd.org/psycopg/docs/install.htmlshell
python基礎教程: http://www.runoob.com/python/python-install.htmlcentos
psycopy鏈接postgresql基本API: http://developer.51cto.com/art/201401/426204_all.htmpost
操做步驟:測試
環境:CentOS6.3 32bitfetch
安裝包版本選擇。ui
首先根據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 這個是系統帶的。