postgreデータベースpython操作
1919 ワード
1. python connect postgre
https://wiki.postgresql.org/wiki/Using_psycopg2_with_PostgreSQL Using psycopg2 with PostgreSQL
https://wiki.postgresql.org/wiki/Using_psycopg2_with_PostgreSQL Using psycopg2 with PostgreSQL
import psycopg2
import pprint
# get a connection, if a connect cannot be made an exception will be raised here
conn = psycopg2.connect("dbname='db1' user='postgres' host='localhost' password='123'")# conn.cursor will return a cursor object, you can use this cursor to perform queries
cursor = conn.cursor()
# execute Query
cursor.execute("SELECT * FROM department")
# retrieve the records from the database
records = cursor.fetchall()
#records = cursor.fetchone()
# print out the records using pretty print
pprint.pprint(records)