forked from django-cms/django-cms-quickstart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkdb.py
71 lines (55 loc) · 2.13 KB
/
checkdb.py
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python
import socket
import time
import os
import sys
import psycopg2
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
# conn=psycopg2.connect(host=sys.argv[1],database=sys.argv[2], user=sys.argv[3], password=sys.argv[4], port=sys.argv[5])
createuser = """CREATE USER %s WITH PASSWORD '%s';
ALTER USER %s SUPERUSER;""" % (sys.argv[3], sys.argv[4],sys.argv[3])
checkuser = """SELECT COUNT(*) = 0 FROM pg_roles WHERE rolname='%s';""" % (sys.argv[3])
createdatabase = """CREATE DATABASE %s WITH OWNER = '%s';""" % (sys.argv[2], sys.argv[3])
checkdatabase = """SELECT COUNT(*) = 0 FROM pg_catalog.pg_database WHERE datname = '%s';""" % (sys.argv[2])
try:
conn=None
port = int(sys.argv[5]) # 5432
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
while True:
try:
s.connect((sys.argv[1], port))
s.close()
break
except socket.error as ex:
print(ex)
time.sleep(0.3)
# conn=psycopg2.connect(host="localhost",database="postgres", user="postgres", password="" , port="5432")
conn=psycopg2.connect(host=sys.argv[1],database="postgres", user=sys.argv[6], password=sys.argv[7], port=sys.argv[5])
cursor = conn.cursor()
print ("database connection succesful")
cursor.execute(checkuser)
user_not_exist_row = cursor.fetchone()
user_not_exist = user_not_exist_row[0]
if user_not_exist:
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
print (createuser)
cursor.execute(createuser)
else:
print ("user exists")
cursor.execute(checkdatabase)
db_not_exist_row = cursor.fetchone()
db_not_exist = db_not_exist_row[0]
if db_not_exist:
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
print (createdatabase)
cursor.execute(createdatabase)
else:
print ("database exists")
except (Exception, psycopg2.Error) as error :
print ("Error while connecting to PostgreSQL", error)
finally:
#closing database connection.
if(conn):
cursor.close()
conn.close()
print("PostgreSQL connection is closed")