安装前依赖:
ubuntu
python 2.7
- apt-get install python-dev
- apt-get install postgresql
- apt-get install postgresql-server-dev-all
centos
python 2.7
- yum install python-devel
- yum install postgresql
- yum install postgresql-devel
安装PyGreSQL:
pip install PyGreSQL
测试是否安装成功
In [2]: import pg
In [3]:
代码
#!/usr/bin/env python
import pg
def pgTest():
try:
pgdb_conn = pg.connect("host=xxxx port=5432 dbname=tpc user=gpadmin")
except Exception, e:
print e.args[0]
return
sql_desc = "select * from call_center;"
for row in pgdb_conn.query(sql_desc).dictresult():
print row
pgdb_conn.close()
if __name__ == '__main__':
pgTest()