如何使用Python操纵Postgres数据库

时间:2023-03-08 18:50:14

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