导航

如何使用Python操纵Postgres数据库

Posted on 2018-09-25 11:49  许爱琪  阅读(205)  评论(0编辑  收藏  举报

pip install psycopg2 psycopg2-binary

 

#!/usr/bin/python

import psycopg2
conn = psycopg2.connect(database="test", user="postgres", password="postgres", host="10.200.22.110", port="5432")
print "Opened database successfully"


cur = conn.cursor()
cur.execute("SELECT id, name from test")
rows = cur.fetchall()
for row in rows:
print "ID = ", row[0]
print "NAME = ", row[1], "\n"

print "Operation done successfully";
conn.close()