#!/usr/bin/env node import pg from "pg"; const url = process.env.DATABASE_URL; if (!url) { console.error( "[DB Check] ERROR: DATABASE_URL is not set.\n" + " Make sure your .env file (or environment) defines DATABASE_URL.\n" + " Example: DATABASE_URL=postgres://user:password@host:5432/dbname" ); process.exit(1); } let host = ""; try { host = new URL(url).host; } catch { console.error( "[DB Check] ERROR: DATABASE_URL appears to be malformed.\n" + " Expected format: postgres://user:password@host:5432/dbname\n" + " Check that DATABASE_URL is set correctly in your .env file or environment." ); process.exit(1); } const client = new pg.Client({ connectionString: url }); try { await client.connect(); await client.query("SELECT 1"); await client.end(); console.log(`[DB Check] Successfully connected to database at ${host}`); } catch (err) { console.error( `[DB Check] ERROR: Cannot connect to the database at ${host}.\n` + " Possible causes:\n" + " - The database server is not running or not reachable\n" + " - The credentials in DATABASE_URL are wrong\n" + " - A firewall or network rule is blocking the connection\n" + ` Details: ${err.message}` ); process.exit(1); }