PostgreSQL – How to Retrieve the Size of A Database
To get the size of a PostgreSQL database, you can use a few different methods, including SQL queries and command-line utilities. Here are a couple of approaches:
Using SQL Query
You can use the following SQL query to get the size of a specific database in PostgreSQL:
SELECT pg_size_pretty(pg_database_size('your_database_name')) AS database_size;
where your_database_name
is the name of the database for which you want to retrieve the size. This query uses the pg_database_size
function to get the size in bytes and then formats it using pg_size_pretty
to make it more human-readable.
Using Command-Line Utility
You can also use the pg_size
utility from the command line to get the size of a PostgreSQL database. Open a terminal and run the following command:
pg_size -d your_database_name
where your_database_name
is the name of the database for which you want to retrieve the size. The pg_size
utility will provide information about the database’s size, including its total size on disk.