EN
Node.js - PostgreSQL - check database size
0 points
In this article, we would like to show you how to check Postgres database size in Node.js.
xxxxxxxxxx
1
const { Client } = require('pg');
2
3
const client = new Client({
4
host: '127.0.0.1',
5
user: 'postgres',
6
password: 'password',
7
port: 5432,
8
});
9
10
const getDatabaseSize = async (databaseName) => {
11
const query = `SELECT pg_size_pretty(pg_database_size($1));`;
12
try {
13
await client.connect(); // gets connection
14
const { rows } = await client.query(query, [databaseName]); // sends query
15
console.log(rows);
16
} catch (error) {
17
console.error(error.stack);
18
return false;
19
} finally {
20
await client.end(); // closes connection
21
}
22
};
23
24
getDatabaseSize('database_name');
Result:
xxxxxxxxxx
1
[ { pg_size_pretty: '8245 kB' } ]