Python: PostgreSQL

ascertain / 2023-05-03 / 原文

 

connection

import psycopg2
from psycopg2 import Error, connection, cursor

conn: connection | None = None
c1: cursor | None = None

try:
    conn = psycopg2.connect(host='localhost', port=5432, user='postgres',
                            password='postgres', database='entail')
    c1 = conn.cursor()
    print(conn.get_dsn_parameters())
    c1.execute('select version()')
    print(c1.fetchall())
except (Exception, Error) as e:
    print(e)
finally:
    if connection and not connection.closed:
        if not c1.closed:
            c1.close()
        conn.close()

 

create table