db connection

This commit is contained in:
root
2026-07-13 18:21:25 +09:00
parent 7130226f26
commit 3b156cf8f7
2 changed files with 22 additions and 0 deletions
+22
View File
@@ -0,0 +1,22 @@
import psycopg2
import os
def get_db_connection():
db_uri = os.getenv("DB_URI")
print("test")
if not db_uri:
raise ValueError("DB_URI 환경변수 없음.")
try:
conn = psycopg2.connect(db_uri)
cursor = conn.cursor()
cursor.execute("SELECT version();")
db_version = cursor.fetchone()
print(f"Connected to the database. Version: {db_version[0]}")
cursor.close()
conn.close()
except Exception as e:
print(f"Error connecting to the database: {e}")
raise