Skip to content

Instantly share code, notes, and snippets.

@anddam
Last active May 9, 2018 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anddam/6a53e926df7e5e3a148bbec26816711b to your computer and use it in GitHub Desktop.
Save anddam/6a53e926df7e5e3a148bbec26816711b to your computer and use it in GitHub Desktop.
# coding: utf-8
from datetime import datetime
from time import sleep
from sys import exit
from psycopg2 import connect, OperationalError
def wait_for_database(delay=4, retries=10, host='db', dbname='postgres',
user='postgres'):
for _ in range(retries):
try:
connection = connect(host=host, dbname=dbname, user=user)
except OperationalError:
print("Couldn't connect to PostgreSQL at <%s>, retrying in %d s." %
(datetime.now(), delay))
sleep(delay)
else:
print("Succesfully connected to PostgreSQL.")
connection.close()
return 0
print("Error: maximum number of retries (%d) reached." % retries)
return -1
if __name__ == '__main__':
exit(wait_for_database())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment