0

I've noticed that on occasions where I've run my django project without the PostgreSQL server available, the errors produced are fairly cryptic and often appear to be generated by deep django internals as these are the functions actually connecting to the backend.

Is there a simple clean(and DRY) way to test the server is running.

Where is the best place to put project level start up checks?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.