Skip to content

Instantly share code, notes, and snippets.

@siegy22
Last active February 7, 2017 11:19
Show Gist options
  • Save siegy22/0b41fe1416c8f7aea4ccc6a90e5cd3d5 to your computer and use it in GitHub Desktop.
Save siegy22/0b41fe1416c8f7aea4ccc6a90e5cd3d5 to your computer and use it in GitHub Desktop.
django management task to create the database
import os
from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
class Command(BaseCommand):
help = "Creates your database using psql command"
def add_arguments(self, parser):
parser.add_argument(
'-R', '--router', action='store', dest='router', default='default',
help='Use this router-database other then defined in settings.py')
def handle(self, *args, **options):
router = options.get('router')
dbinfo = settings.DATABASES.get(router)
if dbinfo is None:
raise CommandError("Unknown database router %s" % router)
dbname = dbinfo.get('NAME')
dbname = dbinfo.get('NAME')
drop_command = "CREATE DATABASE {0};".format(dbname)
os.system('psql postgres -q -c "{0}"'.format(drop_command))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment