File size: 812 Bytes
f283c78 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import "dotenv/config";
import pg from "pg";
const required = ["DATABASE_HOST", "DATABASE_USER", "DATABASE_PASSWORD"];
const missing = required.filter((name) => !process.env[name]);
if (missing.length > 0) {
console.error(`Missing required env vars: ${missing.join(", ")}`);
process.exit(1);
}
const client = new pg.Client({
host: process.env.DATABASE_HOST,
port: Number(process.env.DATABASE_PORT || 5432),
user: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE_DBNAME || "postgres",
ssl: { rejectUnauthorized: false },
connectionTimeoutMillis: 8000,
});
try {
await client.connect();
const result = await client.query("select current_database() as db, now() as now");
console.log(result.rows[0]);
} finally {
await client.end();
}
|