使用Python连接postgresql数据库

来源:互联网 发布:金融类游戏知乎 编辑:程序博客网 时间:2024/06/09 12:23

使用Python连接postgresql数据库的配置非常简单,仅仅需要三步:
1、导入psycopg2包;
2、设置连接
3、取数据

#-*- coding:utf-8 -*-import psycopg2conn = psycopg2.connect(database='aa',user='username',password='123456',host='192.168.131.222',port='8888')cur = conn.cursor()cur.execute("SELECT * FROM table1 LIMIT 10")rows = cur.fetchall()print(rows)conn.commit()cur.close()conn.close()

 

1 0