-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestConnection.ts
41 lines (35 loc) · 979 Bytes
/
testConnection.ts
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
30
31
32
33
34
35
36
37
38
39
40
41
const { Pool } = require("pg")
const fs = require("fs")
require("dotenv").config() // Ensure environment variables are loaded
const caCertBase64 = process.env.RDS_CA_CERT_BASE64
if (!caCertBase64) {
throw new Error("RDS_CA_CERT_BASE64 environment variable is not set.")
}
let caCert
try {
caCert = Buffer.from(caCertBase64, "base64").toString("utf-8")
} catch (error) {
throw new Error("Failed to decode RDS_CA_CERT_BASE64.")
}
const pool = new Pool({
host: process.env.RDS_HOST,
port: Number(process.env.RDS_PORT),
database: process.env.RDS_DATABASE,
user: process.env.RDS_USER,
password: process.env.RDS_PASSWORD,
ssl: {
ca: caCert,
rejectUnauthorized: true,
},
})
async function testConnection() {
try {
const result = await pool.query("SELECT NOW()")
console.log("Connection successful:", result.rows[0])
process.exit(0)
} catch (err) {
console.error("Connection failed:", err)
process.exit(1)
}
}
testConnection()