首页 > 代码库 > postgre 数据库 python操作

postgre 数据库 python操作

1. python connect postgre

https://wiki.postgresql.org/wiki/Using_psycopg2_with_PostgreSQL          Using psycopg2 with PostgreSQL

import psycopg2import pprint
# get a connection, if a connect cannot be made an exception will be raised hereconn = psycopg2.connect("dbname=‘db1‘ user=‘postgres‘ host=‘localhost‘ password=‘123‘")# conn.cursor will return a cursor object, you can use this cursor to perform queriescursor = conn.cursor()# execute Querycursor.execute("SELECT * FROM department")# retrieve the records from the databaserecords = cursor.fetchall()#records = cursor.fetchone()# print out the records using pretty printpprint.pprint(records)

 

postgre 数据库 python操作