0

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?

2
  • Are you trying to drop the whole database or a single table? Commented Jan 5, 2017 at 0:21
  • Where does this code sit in the project? It's a difficult thing to help with because a database connection could come from anywhere. Commented Jan 5, 2017 at 7:45

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.