-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathwait-for-psql.py
More file actions
executable file
·44 lines (37 loc) · 1.26 KB
/
Copy pathwait-for-psql.py
File metadata and controls
executable file
·44 lines (37 loc) · 1.26 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
#!/usr/bin/env python3
import argparse
import psycopg2
import sys
import time
if __name__ == '__main__':
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('--db_host', required=True)
arg_parser.add_argument('--db_port', required=True)
arg_parser.add_argument('--db_user', required=True)
arg_parser.add_argument('--db_password', required=True)
arg_parser.add_argument('--timeout', type=int, default=5)
args = arg_parser.parse_args()
timeout = args.timeout
start_time = time.time()
last_error = None
while True:
try:
conn = psycopg2.connect(
user=args.db_user,
host=args.db_host,
port=args.db_port,
password=args.db_password,
dbname='postgres'
)
print("Database connection successful!")
conn.close()
break
except psycopg2.OperationalError as e:
last_error = e
print(f"Connection failed: {e}. Retrying...")
if (time.time() - start_time) > timeout:
print(f"Failed to connect to the database after {timeout} seconds.")
if last_error:
print(f"Last error: {last_error}")
break
time.sleep(1)