-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_db.py
More file actions
53 lines (42 loc) · 1.51 KB
/
Copy pathcreate_db.py
File metadata and controls
53 lines (42 loc) · 1.51 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os
import sys
import psycopg2
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
def create_database():
"""Create the PostgreSQL database"""
# Get database URL from environment variables
database_url = os.getenv('DATABASE_URL')
if not database_url:
print("Error: DATABASE_URL environment variable not set.")
sys.exit(1)
# Parse the database URL
try:
# Extract the database name from the URL
db_parts = database_url.split('/')
db_name = db_parts[-1]
# Create a connection string without the database name
conn_string = '/'.join(db_parts[:-1]) + '/postgres'
# Connect to the default postgres database
conn = psycopg2.connect(conn_string)
conn.autocommit = True
cursor = conn.cursor()
# Check if the database already exists
cursor.execute(f"SELECT 1 FROM pg_database WHERE datname = '{db_name}'")
exists = cursor.fetchone()
if not exists:
# Create the database
cursor.execute(f"CREATE DATABASE {db_name}")
print(f"Database '{db_name}' created successfully.")
else:
print(f"Database '{db_name}' already exists.")
# Close the connection
cursor.close()
conn.close()
return True
except Exception as e:
print(f"Error creating database: {str(e)}")
return False
if __name__ == '__main__':
create_database()