import socket
import time
import os

port = int(os.environ.get("SQL_PORT")) # 5432
db_name = os.environ.get("SQL_HOST")
print(f"Waiting for database at {db_name}:{port}")

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
while True:
    try:
        s.connect((db_name, port))
        s.close()
        break
    except socket.error:
        time.sleep(0.1)

print(f"Database active")