6

I want to see total database size and data occupied size in postgres. I need query (don't want any third application to check) to check that because based on the query i will develop some report. Thanks in advance.

3
  • 1
    select pg_database_size('<db_name>'); --- >in bytes. Also, select pg_size_pretty(pg_database_size('<db_name>')); -->- in text with units Commented Jan 25, 2022 at 4:47
  • 1
    that query used to check total database size or data occupied size? . thanks for answer Commented Jan 25, 2022 at 4:51
  • That query will give you overall size of the database which includes the relation sizes plus the overhead like dead rows in relations and deleted index leaf block entries. To get the size minus the overhead , there are multiple ways. The easiest is to use extension pgstattuple for all relations. pgstatindex for all indexes. Commented Jan 25, 2022 at 5:05

1 Answer 1

17

THe size of database and table in postgres can be found using following command.

To get the databse size:

SELECT pg_size_pretty( pg_database_size('dbname') ); 

To get the table Size:

 SELECT pg_size_pretty( pg_total_relation_size('tablename') ); 

The below command used to get the list of all databases with their respective size.

SELECT d.datname as Name, pg_catalog.pg_get_userbyid(d.datdba) as Owner, CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT') THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname)) ELSE 'No Access' END as Size FROM pg_catalog.pg_database d order by CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT') THEN pg_catalog.pg_database_size(d.datname) ELSE NULL END desc LIMIT 20; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.