Feedback1 / test_database_connection.py
jyotidabass91's picture
Update test_database_connection.py
2b14975 verified
"""
Test PostgreSQL database connection
"""
import os
from dotenv import load_dotenv
from sqlalchemy import create_engine, text
from sqlalchemy.exc import OperationalError
def test_postgresql_connection():
"""Test PostgreSQL database connection"""
try:
print("πŸ” Testing PostgreSQL connection...")
# Load environment variables
load_dotenv()
# Get database URL
database_url = os.getenv("DATABASE_URL", "")
print(f"πŸ“Š Database URL: {database_url}")
# Create engine
engine = create_engine(
database_url,
pool_pre_ping=True,
pool_size=5,
max_overflow=10,
pool_recycle=300,
echo=False
)
# Test connection
with engine.connect() as connection:
result = connection.execute(text("SELECT 1"))
print("βœ… PostgreSQL connection successful!")
# Test if we can create tables
from src.database import create_tables
create_tables()
print("βœ… Database tables created successfully!")
return True
except OperationalError as e:
print(f"❌ PostgreSQL connection failed: {e}")
return False
except Exception as e:
print(f"❌ Unexpected error: {e}")
return False
if __name__ == "__main__":
print("πŸš€ Testing PostgreSQL Database Connection...")
print("=" * 50)
success = test_postgresql_connection()
print("\n" + "=" * 50)
if success:
print("βœ… PostgreSQL connection test passed!")
else:
print("❌ PostgreSQL connection test failed!")