-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreatedb.py
37 lines (32 loc) · 1.25 KB
/
createdb.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
import psycopg2
import os
from wsgi import application
application = application
def create_dbs():
print("create_dbs: let's go.")
django_settings = __import__(os.environ['DJANGO_SETTINGS_MODULE'], fromlist='DATABASES')
print("create_dbs: got settings.")
databases = django_settings.DATABASES
for name, db in databases.iteritems():
host = db['HOST']
user = db['USER']
password = db['PASSWORD']
port = db['PORT']
db_name = db['NAME']
db_type = db['ENGINE']
if db_type.endswith('postgresql_psycopg2'):
print 'creating database %s on %s' % (db_name, host)
con = psycopg2.connect(host=host, user=user, password=password, port=port, database='postgres')
con.set_isolation_level(0)
cur = con.cursor()
try:
cur.execute('CREATE DATABASE %s' % db_name)
except psycopg2.ProgrammingError as detail:
print detail
print 'moving right along...'
else:
print("ERROR: {0} is not supported by this script, you will need to create your database by hand.".format(db_type))
if __name__ == '__main__':
print("create_dbs start")
create_dbs()
print("create_dbs all done")