def handle(self, *args, **options): db = settings.DATABASES['default'] engine = db['ENGINE'].split('.')[-1] if engine == 'sqlite3': import os try: logging.info("Unlinking sqlite3 database") os.unlink(db['NAME']) except OSError: pass elif engine == 'mysql': import MySQLdb as Database kwargs = { 'user': db['USER'], 'passwd': db['PASSWORD'], } if db['HOST'].startswith('/'): kwargs['unix_socket'] = db['HOST'] else: kwargs['host'] = db['HOST'] if db['PORT']: kwargs['port'] = int(db['PORT']) connection = Database.connect(**kwargs) drop_query = 'DROP DATABASE IF EXISTS %s' % db['NAME'] create_query = 'CREATE DATABASE %s COLLATE utf8_unicode_ci' % db['NAME'] logging.info('Executing... "' + drop_query + '"') connection.query(drop_query) logging.info('Executing... "' + create_query + '"') connection.query(create_query) logging.info("Reset success")
def handle(self, *args, **options): """ Resets the database for this project. Note: Transaction wrappers are in reverse as a work around for autocommit, anybody know how to do this the right way? """ if options.get('interactive'): confirm = raw_input(""" You have requested a database reset. This will IRREVERSIBLY DESTROY ALL data in the database "%s". Are you sure you want to do this? Type 'yes' to continue, or 'no' to cancel: """ % (settings.DATABASE_NAME, )) else: confirm = 'yes' if confirm != 'yes': print "Reset cancelled." return engine = settings.DATABASE_ENGINE if engine == 'sqlite3': import os try: logging.info("Unlinking sqlite3 database") os.unlink(settings.DATABASE_NAME) except OSError: pass elif engine == 'mysql': import MySQLdb as Database kwargs = { 'user': settings.DATABASE_USER, 'passwd': settings.DATABASE_PASSWORD, } if settings.DATABASE_HOST.startswith('/'): kwargs['unix_socket'] = settings.DATABASE_HOST else: kwargs['host'] = settings.DATABASE_HOST if settings.DATABASE_PORT: kwargs['port'] = int(settings.DATABASE_PORT) connection = Database.connect(**kwargs) drop_query = 'DROP DATABASE IF EXISTS %s' % settings.DATABASE_NAME utf8_support = options.get('no_utf8_support', False) and '' or 'CHARACTER SET utf8' create_query = 'CREATE DATABASE %s %s' % (settings.DATABASE_NAME, utf8_support) logging.info('Executing... "' + drop_query + '"') connection.query(drop_query) logging.info('Executing... "' + create_query + '"') connection.query(create_query) elif engine == 'postgresql' or engine == 'postgresql_psycopg2': if engine == 'postgresql': import psycopg as Database elif engine == 'postgresql_psycopg2': import psycopg2 as Database if settings.DATABASE_NAME == '': from django.core.exceptions import ImproperlyConfigured raise ImproperlyConfigured, "You need to specify DATABASE_NAME in your Django settings file." if settings.DATABASE_USER: conn_string = "user=%s" % (settings.DATABASE_USER) if settings.DATABASE_PASSWORD: conn_string += " password='******'" % settings.DATABASE_PASSWORD if settings.DATABASE_HOST: conn_string += " host=%s" % settings.DATABASE_HOST if settings.DATABASE_PORT: conn_string += " port=%s" % settings.DATABASE_PORT connection = Database.connect(conn_string) connection.set_isolation_level(0) #autocommit false cursor = connection.cursor() drop_query = 'DROP DATABASE %s' % settings.DATABASE_NAME logging.info('Executing... "' + drop_query + '"') try: cursor.execute(drop_query) except Database.ProgrammingError, e: logging.info("Error: " + str(e)) # Encoding should be SQL_ASCII (7-bit postgres default) or prefered UTF8 (8-bit) create_query = (""" CREATE DATABASE %s WITH OWNER = %s ENCODING = 'UTF8' TABLESPACE = pg_default; """ % (settings.DATABASE_NAME, settings.DATABASE_USER)) logging.info('Executing... "' + create_query + '"') cursor.execute(create_query)
def handle(self, *args, **options): """ Resets the database for this project. Note: Transaction wrappers are in reverse as a work around for autocommit, anybody know how to do this the right way? """ if options.get('interactive'): confirm = raw_input(""" You have requested a database reset. This will IRREVERSIBLY DESTROY ALL data in the database "%s". Are you sure you want to do this? Type 'yes' to continue, or 'no' to cancel: """ % (settings.DATABASE_NAME,)) else: confirm = 'yes' if confirm != 'yes': print "Reset cancelled." return engine = settings.DATABASE_ENGINE user = options.get('user', settings.DATABASE_USER) if user==None: user = settings.DATABASE_USER password = options.get('password', settings.DATABASE_PASSWORD) if password==None: password = settings.DATABASE_PASSWORD if engine == 'sqlite3': import os try: logging.info("Unlinking sqlite3 database") os.unlink(settings.DATABASE_NAME) except OSError: pass elif engine == 'mysql': import MySQLdb as Database kwargs = { 'user': user, 'passwd': password, } if settings.DATABASE_HOST.startswith('/'): kwargs['unix_socket'] = settings.DATABASE_HOST else: kwargs['host'] = settings.DATABASE_HOST if settings.DATABASE_PORT: kwargs['port'] = int(settings.DATABASE_PORT) connection = Database.connect(**kwargs) drop_query = 'DROP DATABASE IF EXISTS %s' % settings.DATABASE_NAME utf8_support = options.get('no_utf8_support', False) and '' or 'CHARACTER SET utf8' create_query = 'CREATE DATABASE %s %s' % (settings.DATABASE_NAME, utf8_support) logging.info('Executing... "' + drop_query + '"') connection.query(drop_query) logging.info('Executing... "' + create_query + '"') connection.query(create_query) elif engine == 'postgresql' or engine == 'postgresql_psycopg2': if engine == 'postgresql': import psycopg as Database elif engine == 'postgresql_psycopg2': import psycopg2 as Database if settings.DATABASE_NAME == '': from django.core.exceptions import ImproperlyConfigured raise ImproperlyConfigured, "You need to specify DATABASE_NAME in your Django settings file." database_name = options.get('dbname', 'template1') conn_string = "dbname=%s" % database_name if settings.DATABASE_USER: conn_string += " user=%s" % user if settings.DATABASE_PASSWORD: conn_string += " password='******'" % password if settings.DATABASE_HOST: conn_string += " host=%s" % settings.DATABASE_HOST if settings.DATABASE_PORT: conn_string += " port=%s" % settings.DATABASE_PORT connection = Database.connect(conn_string) connection.set_isolation_level(0) #autocommit false cursor = connection.cursor() drop_query = 'DROP DATABASE %s' % settings.DATABASE_NAME logging.info('Executing... "' + drop_query + '"') try: cursor.execute(drop_query) except Database.ProgrammingError, e: logging.info("Error: "+str(e)) # Encoding should be SQL_ASCII (7-bit postgres default) or prefered UTF8 (8-bit) create_query = (""" CREATE DATABASE %s WITH OWNER = %s ENCODING = 'UTF8' TABLESPACE = %s; """ % (settings.DATABASE_NAME, settings.DATABASE_USER, settings.DEFAULT_TABLESPACE)) logging.info('Executing... "' + create_query + '"') cursor.execute(create_query)
def handle(self, *args, **options): """ Resets the database for this project. Note: Transaction wrappers are in reverse as a work around for autocommit, anybody know how to do this the right way? """ if django.get_version() >= "1.2": got_db_settings = self.set_db_settings(*args, **options) if not got_db_settings: raise CommandError( "You are using Django %s which requires to specify the db-router.\nPlease specify the router by adding --router=<routername> to this command." % django.get_version()) return verbosity = int(options.get('verbosity', 1)) if options.get('interactive'): confirm = raw_input(""" You have requested a database reset. This will IRREVERSIBLY DESTROY ALL data in the database "%s". Are you sure you want to do this? Type 'yes' to continue, or 'no' to cancel: """ % (settings.DATABASE_NAME, )) else: confirm = 'yes' if confirm != 'yes': print "Reset cancelled." return postgis = re.compile('.*postgis') engine = settings.DATABASE_ENGINE user = options.get('user', settings.DATABASE_USER) if user == None: user = settings.DATABASE_USER password = options.get('password', settings.DATABASE_PASSWORD) if password == None: password = settings.DATABASE_PASSWORD if engine == 'sqlite3': import os try: logging.info("Unlinking sqlite3 database") os.unlink(settings.DATABASE_NAME) except OSError: pass elif engine == 'mysql': import MySQLdb as Database kwargs = { 'user': user, 'passwd': password, } if settings.DATABASE_HOST.startswith('/'): kwargs['unix_socket'] = settings.DATABASE_HOST else: kwargs['host'] = settings.DATABASE_HOST if settings.DATABASE_PORT: kwargs['port'] = int(settings.DATABASE_PORT) connection = Database.connect(**kwargs) drop_query = 'DROP DATABASE IF EXISTS `%s`' % settings.DATABASE_NAME utf8_support = options.get('no_utf8_support', False) and '' or 'CHARACTER SET utf8' create_query = 'CREATE DATABASE `%s` %s' % (settings.DATABASE_NAME, utf8_support) logging.info('Executing... "' + drop_query + '"') connection.query(drop_query) logging.info('Executing... "' + create_query + '"') connection.query(create_query) elif engine == 'postgresql' or engine == 'postgresql_psycopg2' or postgis.match( engine): if engine == 'postgresql': import psycopg as Database elif engine == 'postgresql_psycopg2' or postgis.match(engine): import psycopg2 as Database if settings.DATABASE_NAME == '': from django.core.exceptions import ImproperlyConfigured raise ImproperlyConfigured( "You need to specify DATABASE_NAME in your Django settings file." ) database_name = options.get('dbname', 'template1') if options.get('dbname') == None: database_name = 'template1' conn_string = "dbname=%s" % database_name if settings.DATABASE_USER: conn_string += " user=%s" % user if settings.DATABASE_PASSWORD: conn_string += " password='******'" % password if settings.DATABASE_HOST: conn_string += " host=%s" % settings.DATABASE_HOST if settings.DATABASE_PORT: conn_string += " port=%s" % settings.DATABASE_PORT connection = Database.connect(conn_string) connection.set_isolation_level(0) # autocommit false cursor = connection.cursor() drop_query = 'DROP DATABASE %s' % settings.DATABASE_NAME logging.info('Executing... "' + drop_query + '"') try: cursor.execute(drop_query) except Database.ProgrammingError, e: logging.info("Error: %s" % str(e)) # Encoding should be SQL_ASCII (7-bit postgres default) or prefered UTF8 (8-bit) create_query = """CREATE DATABASE %s WITH OWNER = %s ENCODING = 'UTF8' """ % ( settings.DATABASE_NAME, settings.DATABASE_USER) if postgis.match(engine): create_query += 'TEMPLATE = template_postgis ' if settings.DEFAULT_TABLESPACE: create_query += 'TABLESPACE = %s;' % ( settings.DEFAULT_TABLESPACE) else: create_query += ';' logging.info('Executing... "' + create_query + '"') cursor.execute(create_query)
def handle(self, *args, **options): """ Resets a database. Note: Transaction wrappers are in reverse as a work around for autocommit, anybody know how to do this the right way? """ engine = settings.DATABASE_ENGINE if engine == 'sqlite3': import os try: logging.info("Unlinking sqlite3 database") os.unlink(settings.DATABASE_NAME) except OSError: pass elif engine == 'mysql': import MySQLdb as Database kwargs = { 'user': settings.DATABASE_USER, 'passwd': settings.DATABASE_PASSWORD, } if settings.DATABASE_HOST.startswith('/'): kwargs['unix_socket'] = settings.DATABASE_HOST else: kwargs['host'] = settings.DATABASE_HOST if settings.DATABASE_PORT: kwargs['port'] = int(settings.DATABASE_PORT) connection = Database.connect(**kwargs) drop_query = 'DROP DATABASE IF EXISTS %s' % settings.DATABASE_NAME create_query = 'CREATE DATABASE %s' % settings.DATABASE_NAME logging.info('Executing... "' + drop_query + '"') connection.query(drop_query) logging.info('Executing... "' + create_query + '"') connection.query(create_query) elif engine == 'postgresql' or engine == 'postgresql_psycopg2': if engine == 'postgresql': import psycopg as Database elif engine == 'postgresql_psycopg2': import psycopg2 as Database if settings.DATABASE_NAME == '': from django.core.exceptions import ImproperlyConfigured raise ImproperlyConfigured, "You need to specify DATABASE_NAME in your Django settings file." if settings.DATABASE_USER: conn_string = "user=%s" % (settings.DATABASE_USER) if settings.DATABASE_PASSWORD: conn_string += " password='******'" % settings.DATABASE_PASSWORD if settings.DATABASE_HOST: conn_string += " host=%s" % settings.DATABASE_HOST if settings.DATABASE_PORT: conn_string += " port=%s" % settings.DATABASE_PORT connection = Database.connect(conn_string) connection.set_isolation_level(0) #autocommit false cursor = connection.cursor() drop_query = 'DROP DATABASE %s' % settings.DATABASE_NAME logging.info('Executing... "' + drop_query + '"') try: cursor.execute(drop_query) except Database.ProgrammingError, e: logging.info("Error: "+str(e)) # Encoding should be SQL_ASCII (7-bit postgres default) or prefered UTF8 (8-bit) create_query = (""" CREATE DATABASE %s WITH OWNER = %s ENCODING = 'UTF8' TABLESPACE = pg_default TEMPLATE= %s; """ % (settings.DATABASE_NAME, settings.DATABASE_USER, 'geo_template')) logging.info('Executing... "' + create_query + '"') cursor.execute(create_query)
def handle(self, *args, **options): """ Resets a database. Note: Transaction wrappers are in reverse as a work around for autocommit, anybody know how to do this the right way? """ print settings engine = settings.DATABASES['default']['ENGINE'] name = settings.DATABASES['default']['NAME'] user = settings.DATABASES['default']['USER'] passwd = settings.DATABASES['default']['PASSWORD'] host = settings.DATABASES['default']['HOST'] port = settings.DATABASES['default']['PORT'] if engine == 'django.db.backends.sqlite3': import os try: logging.info("Unlinking sqlite3 database") os.unlink(name) except OSError: pass elif engine == 'django.db.backends.mysql': import MySQLdb as Database kwargs = { 'user': user, 'passwd': passwd, } if host.startswith('/'): kwargs['unix_socket'] = host else: kwargs['host'] = host if port: kwargs['port'] = int(port) connection = Database.connect(**kwargs) drop_query = 'DROP DATABASE IF EXISTS %s' % name create_query = 'CREATE DATABASE %s' % name logging.info('Executing... "' + drop_query + '"') connection.query(drop_query) logging.info('Executing... "' + create_query + '"') connection.query(create_query) elif engine == 'django.db.backends.postgresql_psycopg2': import psycopg2 as Database if name == '': from django.core.exceptions import ImproperlyConfigured raise ImproperlyConfigured, "You need to specify DATABASE_NAME in your Django settings file." if user: conn_string = "user=%s" % (user) if passwd: conn_string += " password='******'" % passwd if host: conn_string += " host=%s" % host if port: conn_string += " port=%s" % port connection = Database.connect(conn_string) connection.set_isolation_level(0) #autocommit false cursor = connection.cursor() drop_query = 'DROP DATABASE %s' % name logging.info('Executing... "' + drop_query + '"') try: cursor.execute(drop_query) except Database.ProgrammingError, e: logging.info("Error: " + str(e)) # Encoding should be SQL_ASCII (7-bit postgres default) or prefered UTF8 (8-bit) create_query = (""" CREATE DATABASE %s WITH OWNER = %s ENCODING = 'UTF8' TABLESPACE = pg_default; """ % (name, user)) logging.info('Executing... "' + create_query + '"') cursor.execute(create_query)
def handle(self, *args, **options): """ Resets a database. """ database = settings.DATABASES['default'] if 'sqlite3' in database['ENGINE']: import os try: logging.info("Unlinking sqlite3 database") os.unlink(database['NAME']) except OSError: pass elif 'mysql' in database['ENGINE']: import MySQLdb as Database kwargs = { 'user': database['USER'], 'passwd': database['PASSWORD'], } if database['HOST'].startswith('/'): kwargs['unix_socket'] = database['HOST'] else: kwargs['host'] = database['HOST'] if database['PORT']: kwargs['port'] = int(database['PORT']) connection = Database.connect(**kwargs) drop_query = 'DROP DATABASE IF EXISTS %%s' %% database['NAME'] create_query = 'CREATE DATABASE %%s' %% database['NAME'] logging.info('Executing... "' + drop_query + '"') connection.query(drop_query) logging.info('Executing... "' + create_query + '"') connection.query(create_query) elif 'postgresql' in database['ENGINE']: if 'postgresql_psycopg2' in database['ENGINE']: import psycopg2 as Database else: import psycopg as Database if database['NAME'] == '': from django.core.exceptions import ImproperlyConfigured raise ImproperlyConfigured, "You need to specify DATABASE_NAME in your Django settings file." if database['USER']: conn_string = "user=%%s" %% (database['USER']) if database['PASSWORD']: conn_string += " password='******'" %% database['PASSWORD'] if database['HOST']: conn_string += " host=%%s" %% database['HOST'] if database['PORT']: conn_string += " port=%%s" %% database['PORT'] connection = Database.connect(conn_string) connection.set_isolation_level(0) #autocommit false cursor = connection.cursor() drop_query = 'DROP DATABASE %%s' %% database['NAME'] logging.info('Executing... "' + drop_query + '"') try: cursor.execute(drop_query) except Database.ProgrammingError, e: logging.info("Error: "+str(e)) # Encoding should be SQL_ASCII (7-bit postgres default) or prefered UTF8 (8-bit) create_query = (""" CREATE DATABASE %%s WITH OWNER = %%s ENCODING = 'UTF8' TABLESPACE = pg_default; """ %% (database['NAME'], database['USER'])) logging.info('Executing... "' + create_query + '"') cursor.execute(create_query)
def handle(self, *args, **options): """ Resets a database. Note: Transaction wrappers are in reverse as a work around for autocommit, anybody know how to do this the right way? """ print settings engine = settings.DATABASES['default']['ENGINE'] name = settings.DATABASES['default']['NAME'] user = settings.DATABASES['default']['USER'] passwd = settings.DATABASES['default']['PASSWORD'] host = settings.DATABASES['default']['HOST'] port = settings.DATABASES['default']['PORT'] if engine == 'django.db.backends.sqlite3': import os try: logging.info("Unlinking sqlite3 database") os.unlink(name) except OSError: pass elif engine == 'django.db.backends.mysql': import MySQLdb as Database kwargs = { 'user': user, 'passwd': passwd, } if host.startswith('/'): kwargs['unix_socket'] = host else: kwargs['host'] = host if port: kwargs['port'] = int(port) connection = Database.connect(**kwargs) drop_query = 'DROP DATABASE IF EXISTS %s' % name create_query = 'CREATE DATABASE %s' % name logging.info('Executing... "' + drop_query + '"') connection.query(drop_query) logging.info('Executing... "' + create_query + '"') connection.query(create_query) elif engine == 'django.db.backends.postgresql_psycopg2': import psycopg2 as Database if name == '': from django.core.exceptions import ImproperlyConfigured raise ImproperlyConfigured, "You need to specify DATABASE_NAME in your Django settings file." if user: conn_string = "user=%s" % (user) if passwd: conn_string += " password='******'" % passwd if host: conn_string += " host=%s" % host if port: conn_string += " port=%s" % port connection = Database.connect(conn_string) connection.set_isolation_level(0) #autocommit false cursor = connection.cursor() drop_query = 'DROP DATABASE %s' % name logging.info('Executing... "' + drop_query + '"') try: cursor.execute(drop_query) except Database.ProgrammingError, e: logging.info("Error: "+str(e)) # Encoding should be SQL_ASCII (7-bit postgres default) or prefered UTF8 (8-bit) create_query = (""" CREATE DATABASE %s WITH OWNER = %s ENCODING = 'UTF8' TABLESPACE = pg_default; """ % (name, user)) logging.info('Executing... "' + create_query + '"') cursor.execute(create_query)
def handle(self, *args, **options): """ Resets the database for this project. Note: Transaction wrappers are in reverse as a work around for autocommit, anybody know how to do this the right way? """ verbosity = int(options.get('verbosity', 1)) database = options.get('database') try: db = settings.DATABASES[database] except KeyError: raise Exception('Given database (%s) not found in settings' % database) if options.get('interactive'): confirm = raw_input(""" You have requested a database reset. This will IRREVERSIBLY DESTROY ALL data in the database "%s". Are you sure you want to do this? Type 'yes' to continue, or 'no' to cancel: """ % (db['NAME'],)) else: confirm = 'yes' if confirm != 'yes': print "Reset cancelled." return engine = db['ENGINE'].split('.')[-1] if engine == 'sqlite3': import os try: logging.info("Unlinking sqlite3 database") os.unlink(db['NAME']) except OSError: pass elif engine == 'mysql': import MySQLdb as Database kwargs = { 'user': db['USER'], 'passwd': db['PASSWORD'], } if db['HOST'].startswith('/'): kwargs['unix_socket'] = db['HOST'] else: kwargs['host'] = db['HOST'] if 'PORT' in db and db['PORT']: kwargs['port'] = int(db['PORT']) connection = Database.connect(**kwargs) drop_query = 'DROP DATABASE IF EXISTS %s' % db['NAME'] charset = '' if 'OPTIONS' in db: if 'init_command' in db['OPTIONS']: connection.query(db['OPTIONS']['init_command']) if 'charset' in db['OPTIONS']: charset = 'CHARACTER SET %s' % db['OPTIONS']['charset'] create_query = 'CREATE DATABASE %s %s' % (db['NAME'], charset) logging.info('Executing... "' + drop_query + '"') connection.query(drop_query) logging.info('Executing... "' + create_query + '"') connection.query(create_query) elif engine in ('postgresql', 'postgresql_psycopg2'): if engine == 'postgresql': import psycopg as Database elif engine == 'postgresql_psycopg2': import psycopg2 as Database if db['NAME'] == '': from django.core.exceptions import ImproperlyConfigured raise ImproperlyConfigured, "You need to specify NAME of db in your Django settings file." if 'USER' in db: conn_string = "user=%s" % (db['USER']) if 'PASSWORD' in db: conn_string += " password='******'" % db['PASSWORD'] if 'HOST' in db: conn_string += " host=%s" % db['HOST'] if 'PORT' in db: conn_string += " port=%s" % db['PORT'] connection = Database.connect(conn_string) connection.set_isolation_level(0) #autocommit false cursor = connection.cursor() drop_query = 'DROP DATABASE %s' % db['NAME'] logging.info('Executing... "' + drop_query + '"') try: cursor.execute(drop_query) except Database.ProgrammingError, e: logging.info("Error: "+str(e)) # Encoding should be SQL_ASCII (7-bit postgres default) or prefered UTF8 (8-bit) create_query = (""" CREATE DATABASE %s WITH OWNER = %s ENCODING = 'UTF8' TABLESPACE = pg_default; """ % (settings.DATABASE_NAME, settings.DATABASE_USER)) logging.info('Executing... "' + create_query + '"') cursor.execute(create_query)
def handle(self, *args, **options): """ Resets a database. Note: Transaction wrappers are in reverse as a work around for autocommit, anybody know how to do this the right way? """ engine = settings.DATABASE_ENGINE if engine == 'sqlite3': import os try: logging.info("Unlinking sqlite3 database") os.unlink(settings.DATABASE_NAME) except OSError: pass elif engine == 'mysql': import MySQLdb as Database kwargs = { 'user': settings.DATABASE_USER, 'passwd': settings.DATABASE_PASSWORD, } if settings.DATABASE_HOST.startswith('/'): kwargs['unix_socket'] = settings.DATABASE_HOST else: kwargs['host'] = settings.DATABASE_HOST if settings.DATABASE_PORT: kwargs['port'] = int(settings.DATABASE_PORT) connection = Database.connect(**kwargs) drop_query = 'DROP DATABASE IF EXISTS %s' % settings.DATABASE_NAME create_query = 'CREATE DATABASE %s' % settings.DATABASE_NAME logging.info('Executing... "' + drop_query + '"') connection.query(drop_query) logging.info('Executing... "' + create_query + '"') connection.query(create_query) elif engine == 'postgresql' or engine == 'postgresql_psycopg2': if engine == 'postgresql': import psycopg as Database elif engine == 'postgresql_psycopg2': import psycopg2 as Database if settings.DATABASE_NAME == '': from django.core.exceptions import ImproperlyConfigured raise ImproperlyConfigured, "You need to specify DATABASE_NAME in your Django settings file." if settings.DATABASE_USER: conn_string = "user=%s" % (settings.DATABASE_USER) if settings.DATABASE_PASSWORD: conn_string += " password='******'" % settings.DATABASE_PASSWORD if settings.DATABASE_HOST: conn_string += " host=%s" % settings.DATABASE_HOST if settings.DATABASE_PORT: conn_string += " port=%s" % settings.DATABASE_PORT connection = Database.connect(conn_string) connection.set_isolation_level(0) #autocommit false cursor = connection.cursor() drop_query = 'DROP DATABASE %s' % settings.DATABASE_NAME logging.info('Executing... "' + drop_query + '"') try: cursor.execute(drop_query) except Database.ProgrammingError, e: logging.info("Error: " + str(e)) # Encoding should be SQL_ASCII (7-bit postgres default) or prefered UTF8 (8-bit) create_query = (""" CREATE DATABASE %s WITH OWNER = %s ENCODING = 'UTF8' TABLESPACE = pg_default; """ % (settings.DATABASE_NAME, settings.DATABASE_USER)) logging.info('Executing... "' + create_query + '"') cursor.execute(create_query)