[Django]-Django not reconnecting when PostgreSQL dies, custom backend needed?

4👍

I think I figured it out…finally. Not entirely sure what was wrong with my first approach but this one seems to be working a ton better.

class DatabaseWrapper(PostgresWrapper):
    def _cursor(self):
        if self.connection is not None:
            if not self.is_usable():
                self.connection.close()
                self.connection = None
        return super(DatabaseWrapper, self)._cursor()

Edit: Ended up open sourcing this. I am not sure if it is 100% needed, but it is working correctly after restarting the Postgres service on the server. You can find it on pypi as django-postgreconnect, and on GitHub: https://github.com/EccoTheFlintstone/django-postgreconnect.

Leave a comment