Spaces:
Running
Running
| 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()) | |