-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_db.py
More file actions
24 lines (19 loc) · 687 Bytes
/
init_db.py
File metadata and controls
24 lines (19 loc) · 687 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import asyncio
import asyncpg
import os
DATABASE_URL = "postgresql://postgres:postgres@localhost:5434/postgres"
async def create_db():
print(f"Connecting to {DATABASE_URL}...")
try:
conn = await asyncpg.connect(DATABASE_URL)
print("Connected to postgres DB.")
try:
await conn.execute('CREATE DATABASE task_db')
print("Database task_db created successfully.")
except asyncpg.exceptions.DuplicateDatabaseError:
print("Database task_db already exists.")
await conn.close()
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
asyncio.run(create_db())