[Answer]-Django check if postgresql server running

1👍

You can register a signal on class-prepared.
https://docs.djangoproject.com/en/dev/ref/signals/#class-prepared

Than try executing custom sql directly.
https://docs.djangoproject.com/en/dev/topics/db/sql/#executing-custom-sql-directly

If it fails raise your custom exception.

0👍

    import time

    from django.db import connections
    from django.db.utils import OperationalError

    self.stdout.write('Waiting for database...')
            db_conn = None
            while not db_conn:
                try:
                    db_conn = connections['default']
                except OperationalError:
                    self.stdout.write('Database unavailable, waiting 1 second...')
                    time.sleep(1)

            self.stdout.write(self.style.SUCCESS('Database available!'))

you can use this snippet where you need to.
befor accessing database and making any queries you must check if the database is up or not

Leave a comment