-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-database.js
More file actions
executable file
·37 lines (32 loc) · 917 Bytes
/
check-database.js
File metadata and controls
executable file
·37 lines (32 loc) · 917 Bytes
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
#!/usr/bin/env node
import { Pool } from 'pg';
// Function to check database connection
async function checkDatabaseConnection() {
console.log('Testing database connection...');
// Create a database pool
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
});
try {
// Try to connect and run a simple query
const client = await pool.connect();
try {
const result = await client.query('SELECT current_timestamp');
console.log('Database connection successful!');
console.log(`Server time: ${result.rows[0].current_timestamp}`);
return true;
} finally {
client.release();
}
} catch (err) {
console.error('Database connection failed!');
console.error(err.message);
return false;
} finally {
await pool.end();
}
}
// Run the check
checkDatabaseConnection().then(success => {
process.exit(success ? 0 : 1);
});