Languages
[Edit]
EN

Node.js - PostgreSQL - check database size

0 points
Created by:
Brandy-Mccabe
724

In this article, we would like to show you how to check Postgres database size in Node.js.

const { Client } = require('pg');

const client = new Client({
    host: '127.0.0.1',
    user: 'postgres',
    password: 'password',
    port: 5432,
});

const getDatabaseSize = async (databaseName) => {
    const query = `SELECT pg_size_pretty(pg_database_size($1));`;
    try {
        await client.connect(); // gets connection
        const { rows } = await client.query(query, [databaseName]); // sends query
        console.log(rows);
    } catch (error) {
        console.error(error.stack);
        return false;
    } finally {
        await client.end();     // closes connection
    }
};

getDatabaseSize('database_name');

Result: 

[ { pg_size_pretty: '8245 kB' } ]

Alternative titles

  1. Node.js - PostgreSQL How Node.js - PostgreSQL check database size?
  2. Node.js - PostgreSQL How Node.js - PostgreSQL check database size by name parameter?
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Node.js - PostgreSQL - Problems

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join