import psycopg2 import subprocess # establishing the connection conn = psycopg2.connect( database="postgres", user='license_user', password='license_user_password', host='127.0.0.1', port='5432' ) conn.autocommit = True # Creating a cursor object using the cursor() method cursor = conn.cursor() # Preparing query to create a database sql = '''DROP DATABASE license;''' # Creating a database try: cursor.execute(sql) print("Database dropped successfully ->") except psycopg2.ProgrammingError: print("Database license does not exists") sql = '''CREATE database license;''' cursor.execute(sql) print("-> Database created successfully") sql = '''grant all privileges on database license to license_user;''' cursor.execute(sql) print("Database user license_user has all rights to database license") # Closing the connection conn.close() subprocess.run(['python3', 'manage.py', 'makemigrations']) subprocess.run(['python3', 'manage.py', 'migrate'])