how to automatically kill sessions on postgres db in python django, before django tests recreate the db
django recreates our postgres test database on start, however in some cases there are sessions open. I can kill the sessions with
SELECT 'SELECT pg_terminate_backend(' || pg_terminate_backend(pg_stat_activity.pid) || ');'
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'MY_DBNAME'
AND pg_stat_activity.pid <> pg_backend_pid();
but currently, I do that manually as needed from SquirrelSQL. How can this be done automatically before django recreates the DB (very early in the test setup process). THis is needed because otherwise the delete DB fails due to connected sessions.
source https://stackoverflow.com/questions/77645214/how-to-automatically-kill-sessions-on-postgres-db-in-python-django-before-djang
Comments
Post a Comment