| import os | |
| from dotenv import load_dotenv | |
| import psycopg2 | |
| load_dotenv() | |
| print("=" * 60) | |
| print("π Testing Database Connection") | |
| print("=" * 60) | |
| database_url = os.getenv("DATABASE_URL") | |
| if not database_url: | |
| print("β DATABASE_URL not found in .env file!") | |
| exit(1) | |
| print(f"\nπ Connecting to: {database_url[:30]}...{database_url[-20:]}") | |
| try: | |
| print("\nβ³ Attempting connection...") | |
| conn = psycopg2.connect(database_url) | |
| print("β Connection successful!") | |
| # Test a simple query | |
| cursor = conn.cursor() | |
| cursor.execute("SELECT version();") | |
| version = cursor.fetchone() | |
| print(f"\nβ PostgreSQL version: {version[0][:50]}...") | |
| cursor.close() | |
| conn.close() | |
| print("\nπ Database is ready! You can now run: python init_database.py") | |
| except Exception as e: | |
| print(f"\nβ Connection failed!") | |
| print(f"Error: {e}") | |
| print("\nπ‘ Troubleshooting tips:") | |
| print(" 1. Check if your Neon database is active (not paused)") | |
| print(" 2. Verify the DATABASE_URL in .env file") | |
| print(" 3. Make sure there are no quotes around the URL in .env") | |
| print(" 4. Try removing ?channel_binding=require from the URL") | |
| print("=" * 60) | |