File size: 1,217 Bytes
a558c77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import asyncio
import os
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy import text

# Working credentials found: postgres:postgres@localhost:5432/postgres
ADMIN_URL = "postgresql+asyncpg://postgres:postgres@localhost:5432/postgres"
TARGET_DB = "cuatrolabs"

async def setup_database():
    print(f"Connecting to admin DB: {ADMIN_URL}")
    engine = create_async_engine(ADMIN_URL, isolation_level="AUTOCOMMIT")
    
    try:
        async with engine.connect() as conn:
            # Check if database exists
            result = await conn.execute(text(f"SELECT 1 FROM pg_database WHERE datname = '{TARGET_DB}'"))
            exists = result.scalar()
            
            if exists:
                print(f"Database '{TARGET_DB}' already exists.")
            else:
                print(f"Database '{TARGET_DB}' does not exist. Creating...")
                await conn.execute(text(f"CREATE DATABASE {TARGET_DB}"))
                print(f"Database '{TARGET_DB}' created successfully.")
                
    except Exception as e:
        print(f"Error during database setup: {e}")
    finally:
        await engine.dispose()

if __name__ == "__main__":
    asyncio.run(setup_database())