Skip to content
Snippets Groups Projects
sandbox.py 980 B
import configparser
import psycopg2
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
cfg = configparser.ConfigParser()
cfg.read('../config.ini')
assert "POSTGRES" in cfg, "missing POSTGRES in config.ini"
assert "INTERPOLATION" in cfg, "missing INTERPOLATION in config.ini"
param_postgres = cfg["POSTGRES"]

connection = psycopg2.connect(database='postgres', user='postgres', password='postgres', host=param_postgres["host"], port=5432)
connection.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)  # Needs to be in AUTOCOMMIT mode for creating database
cursor = connection.cursor()
cursor.execute("""SELECT 
    pg_terminate_backend(pid) 
FROM 
    pg_stat_activity 
WHERE 
    -- don't kill my own connection!
    pid <> pg_backend_pid()
    -- don't kill the connections to other databases
    AND datname = 'temperatures_berteld_morstein'
    ;""")
connection.commit()
cursor.execute("drop database temperatures_berteld_morstein")
connection.commit()
connection.close()