I'm creating a functionality that allows an admin to drop a database from the Django admin. However, I keep getting errors that a drop database cannot be execute from a transaction block or that the database is open.
I have tried solutions placed here and here, either writing it in a template and executing from SQLAlchemy directly but no luck.
My current code block is this
engine = sa.create_engine('postgresql_string')
session = sessionmaker(bind=engine)()
session.close_all()
engine.dispose()
session.autocommit = True
session.connection().connection.set_isolation_level(0)
session.execute('DROP DATABASE IF EXISTS test_db;')
session.connection().connection.set_isolation_level(1)
But it still will throw an error even when I terminated all activity from pg_stat_activity
, any other ideas on how to implement this?