-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathcheckdb.py
More file actions
41 lines (35 loc) · 1.02 KB
/
Copy pathcheckdb.py
File metadata and controls
41 lines (35 loc) · 1.02 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
import psycopg2
import os
from keys_env import *
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
env = os.getenv('APP_ENV')
if env != 'production':
conn = psycopg2.connect(
dbname="postgres",
user=db_username,
password=db_password,
host=db_host,
)
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
cur = conn.cursor()
dbnames = []
cur.execute("SELECT datname FROM pg_database;")
for dbn in cur.fetchall():
dbnames.append(dbn[0])
if db_name not in dbnames:
cur.execute("CREATE DATABASE {};".format(db_name))
conn.commit()
cur.close()
conn.close()
conn = psycopg2.connect(
dbname=db_name,
user=db_username,
password=db_password,
host=db_host,
)
cur = conn.cursor()
cur.execute("CREATE SCHEMA reopt_api;")
cur.execute("ALTER SCHEMA reopt_api OWNER TO {};".format(db_username))
conn.commit()
cur.close()
conn.close()