-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwait-for-psql.py
More file actions
45 lines (38 loc) · 1.3 KB
/
wait-for-psql.py
File metadata and controls
45 lines (38 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python3
# ABOUTME: Python script to wait for PostgreSQL availability
import os
import sys
import time
import psycopg2
from psycopg2 import OperationalError
def wait_for_psql():
"""Wait for PostgreSQL to become available."""
db_host = os.environ.get('DB_HOST', 'db')
db_port = os.environ.get('DB_PORT', '5432')
db_user = os.environ.get('DB_USER', 'openspp')
db_password = os.environ.get('DB_PASSWORD', 'openspp')
db_name = os.environ.get('DB_NAME', 'postgres')
max_attempts = 24
attempt = 0
print(f"Waiting for PostgreSQL at {db_host}:{db_port}...")
while attempt < max_attempts:
try:
conn = psycopg2.connect(
host=db_host,
port=db_port,
user=db_user,
password=db_password,
database=db_name,
connect_timeout=5
)
conn.close()
print("PostgreSQL is ready!")
return 0
except OperationalError as e:
attempt += 1
print(f"PostgreSQL is unavailable (attempt {attempt}/{max_attempts}) - sleeping")
time.sleep(5)
print("PostgreSQL did not become ready in time", file=sys.stderr)
return 1
if __name__ == "__main__":
sys.exit(wait_for_psql())