const { Client } = require('pg'); require('dotenv').config(); async function testDatabaseConnection() { console.log('=== Database Connection Test ==='); console.log('DATABASE_URL from .env:', process.env.DATABASE_URL); if (!process.env.DATABASE_URL) { console.error('❌ DATABASE_URL not found in environment variables'); console.log('Current environment variables:'); Object.keys(process.env).filter(key => key.includes('DATABASE')).forEach(key => { console.log(`${key}: ${process.env[key]}`); }); return; } const client = new Client({ connectionString: process.env.DATABASE_URL, ssl: false, // Set to true if your database requires SSL }); try { console.log('🔄 Attempting to connect to database...'); await client.connect(); console.log('✅ Successfully connected to database!'); // Test a simple query const result = await client.query('SELECT version()'); console.log('📊 Database version:', result.rows[0].version); // Check if our tables exist const tablesResult = await client.query(` SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_name IN ('user_profiles', 'user_identities') `); console.log('📋 Found tables:', tablesResult.rows.map(row => row.table_name)); if (tablesResult.rows.length === 0) { console.log('⚠️ No user tables found. You may need to run migrations.'); console.log('Run: npm run migrate:up'); } } catch (error) { console.error('❌ Database connection failed:', error.message); console.error('🔍 Error details:', { code: error.code, errno: error.errno, syscall: error.syscall, address: error.address, port: error.port }); if (error.code === 'ENOTFOUND') { console.error('🌐 DNS resolution failed - check if the host is correct'); } else if (error.code === 'ECONNREFUSED') { console.error('🚫 Connection refused - check if database server is running'); } else if (error.code === '28P01') { console.error('🔐 Authentication failed - check username/password'); } else if (error.code === '3D000') { console.error('🗄️ Database does not exist - check database name'); } } finally { await client.end(); } } testDatabaseConnection();