Database Existence Check and Creation

  • Share this:

Code introduction


This function checks if the database exists and creates it if it does not exist.


Technology Stack : SQLAlchemy-Utils

Code Type : Python Function

Code Difficulty : Intermediate


                
                    
from sqlalchemy_utils import database_exists, create_database

def create_db_if_not_exists(engine, db_name):
    """
    Check if the database exists and create it if it does not.
    """
    if not database_exists(engine, db_name):
        create_database(engine, db_name)