Skip to content

Instantly share code, notes, and snippets.

@altendky
Forked from anddam/postgresql-sync.py
Last active May 9, 2018 12:59
Show Gist options
  • Save altendky/7c768d321492258ae883e775a9e4e23f to your computer and use it in GitHub Desktop.
Save altendky/7c768d321492258ae883e775a9e4e23f to your computer and use it in GitHub Desktop.
# coding: utf-8
from datetime import datetime
from time import sleep
from psycopg2 import connect, OperationalError
connected = False
while not connected:
try:
connection = connect(host='db', dbname='postgres', user='postgres')
except OperationalError:
print("Couldn't connect at %s, waiting 4 s ." % datetime.now())
sleep(4)
else:
connected = True
connection.close()
def can_connect(retries=10):
delay = 1
for _ in range(retries):
try:
with connect(host='db', dbname='postgres', user='postgres') as connection:
# test the connection
connection.close()
return True
except OperationalError:
print("Couldn't connect at %s, waiting 4 s ." % datetime.now())
sleep(delay)
delay = min(delay * 2, 60)
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment