Initial commit: SIBU 2.0 MISSION
This commit is contained in:
24
backend/setup_db.py
Normal file
24
backend/setup_db.py
Normal file
@ -0,0 +1,24 @@
|
||||
import asyncio
|
||||
import asyncpg
|
||||
|
||||
async def setup_db():
|
||||
try:
|
||||
# Connect to default postgres DB
|
||||
conn = await asyncpg.connect("postgresql://postgres:postgres@localhost:5432/postgres")
|
||||
|
||||
# Check if sibu exists
|
||||
dbs = await conn.fetch("SELECT datname FROM pg_database WHERE datname = 'sibu'")
|
||||
if not dbs:
|
||||
print("Creating database 'sibu'...")
|
||||
# We can't run CREATE DATABASE inside a transaction
|
||||
await conn.execute("CREATE DATABASE sibu")
|
||||
print("Database 'sibu' created!")
|
||||
else:
|
||||
print("Database 'sibu' already exists.")
|
||||
|
||||
await conn.close()
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(setup_db())
|
||||
Reference in New Issue
Block a user