-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcreate_db.py
34 lines (28 loc) · 915 Bytes
/
create_db.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
import os
import sys
import psycopg2
from psycopg2 import connect
def main():
db_name = os.environ['PGDATABASE']
connection_parameters = {
'host': os.environ['PGHOST'],
'database': 'postgres',
'user': os.environ['PGUSER'],
'password': os.environ['PGPASSWORD']
}
drop_statement = 'DROP DATABASE IF EXISTS {};'.format(db_name)
ddl_statement = 'CREATE DATABASE {};'.format(db_name)
conn = connect(**connection_parameters)
conn.autocommit = True
try:
with conn.cursor() as cursor:
cursor.execute(drop_statement)
cursor.execute(ddl_statement)
conn.close()
sys.stdout.write('Created database environment successfully.\n')
except psycopg2.Error:
raise SystemExit(
'Failed to setup Postgres environment.\n{0}'.format(sys.exc_info())
)
if __name__ == '__main__':
main()