import psycopg2
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT

def create_database():
    try:
        # Connect to PostgreSQL server
        conn = psycopg2.connect(
            "postgresql://postgres:Figur%40tive94%21@localhost:5432/postgres"
        )
        conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
        
        # Create a cursor
        cur = conn.cursor()
        
        # Check if database exists
        cur.execute("SELECT 1 FROM pg_catalog.pg_database WHERE datname = 'artdb'")
        exists = cur.fetchone()
        
        if not exists:
            print("Creating database 'artdb'...")
            cur.execute('CREATE DATABASE artdb')
            print("Database created successfully!")
        else:
            print("Database 'artdb' already exists.")
            
        # Close cursor and connection
        cur.close()
        conn.close()
        
    except Exception as e:
        print(f"Error: {str(e)}")
        raise

if __name__ == '__main__':
    create_database()
