import psycopg2
from config import host, user, password
try:
con = psycopg2.connect(
host = host,
user = user,
password = password,
database = "postgres",
)
cur = con.cursor()
print(con.autocommit)
cur.execute("DROP TABLE IF EXISTS users")
cur.execute("""CREATE TABLE IF NOT EXISTS users (
id INTEGER )""")
cur.execute("INSERT INTO users VALUES (0); INSERT INTO users VALUES (1)")
cur.execute("INSERT INTO users VALUES (2)")
except BaseException as error:
print(error.__class__, error)
finally:
if not cur.closed:
cur.close()
con.close()
print("Соединение с PostgreSQL закрыто")
import psycopg2
from config import host, user, password
try:
con = psycopg2.connect(
host = host,
user = user,
password = password,
database = "postgres",
)
cur = con.cursor()
print(con.autocommit)
cur.execute("DROP TABLE IF EXISTS users")
cur.execute("""CREATE TABLE IF NOT EXISTS users (
id INTEGER )""")
cur.execute("INSERT INTO users VALUES (0); INSERT INTO users VALUES (1)")
cur.execute("INSERT INTO users VALUES (2)")
except BaseException as error:
print(error.__class__, error)
finally:
if not cur.closed:
cur.close()
con.close()
print("Соединение с PostgreSQL закрыто")