Database Existence Check and Creation

  • Share this:

Code introduction


This function checks if a specified database exists and creates it if it does not.


Technology Stack : SQLAlchemy-Utils

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
from sqlalchemy_utils import database_exists, create_database

def create_db_if_not_exists(db_url, db_name):
    """
    Create a database if it does not already exist.
    """
    if not database_exists(db_url + '/' + db_name):
        create_database(db_url + '/' + db_name)
    return True