import psycopg2 import sys import os from datetime import datetime # Get the absolute path to the log directory log_dir = os.path.dirname(os.path.abspath(__file__)) connection_log = os.path.join(log_dir, 'connection_log.txt') error_log = os.path.join(log_dir, 'connection_error.txt') def write_log(message, is_error=False): timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S') log_file = error_log if is_error else connection_log with open(log_file, 'a') as f: f.write(f"[{timestamp}] {message}\n") # Also try printing to stderr for immediate feedback print(message, file=sys.stderr) def test_sync_connection(): write_log("Starting synchronous connection test") try: write_log("Attempting to connect to database...") write_log(f"Using host: juycnkjuzylnbruwaqmp.supabase.co") conn = psycopg2.connect( dbname="postgres", user="postgres", password="Lovyelias5584.", host="juycnkjuzylnbruwaqmp.supabase.co", port="5432", sslmode="require", connect_timeout=30 ) write_log("Connected successfully!") # Test the connection cur = conn.cursor() cur.execute('SELECT version()') version = cur.fetchone() write_log(f"Database version: {version}") cur.close() conn.close() write_log("Connection closed successfully") return True except Exception as e: error_msg = f"Error connecting to database: {str(e)}" write_log(error_msg, is_error=True) return False if __name__ == "__main__": success = test_sync_connection() if not success: sys.exit(1)