How to calculate the PostgreSQL database size

12 November,2019 by Jack Vamvas

Question : How can I calculate the size of a PostgreSQL database using psql?I want to use the command line and execute a sql statement for various checks?

Answer: An easy way to derive the Postgresql database size is to use the    pg_database_size() function. The   pg_database_size() reads the database name and returns the database size in bytes. 

For example : SELECT pg_database_size('my_db_name')

This returns in bytes - which unless you enjoy calculating byte sizes - there is another function you can use which will make these numbers human readable - pg_size_pretty()

Combining these two functions will return the database size in a a human readable method -

SELECT pg_size_pretty( pg_database_size('my_db_name') );

 

 

 


Author: Jack Vamvas (http://www.dba-ninja.com)


Share:

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment on How to calculate the PostgreSQL database size


dba-ninja.com