I have a django project on a Digital Ocean server. From my local machine, I connect to the database through ssh:
ssh -L 63333:localhost:5432 me@my.server
from django.db import connections, connection
for conn in connections.all():
conn.close_if_unusable_or_obsolete()
then call connection.cursor will get a fresh connection, django source code:
def _cursor(self, name=None):
self.ensure_connection()
with self.wrap_database_errors:
return self._prepare_cursor(self.create_cursor(name))