Quepinch

How to check the database size in Postgres using psql command

Dec 16, 2024

There are several ways to check the size of a database in PostgreSQL:

1. Using pg_database_size function: This function returns the size of the database in bytes.

SELECT pg_size_pretty(pg_database_size('your_database_name'));



2. Using pg_total_relation_size function: This function returns the total size of a table, including all its indexes and TOAST data.

SELECT pg_size_pretty(pg_total_relation_size('your_table_name'));



3. Using pg_size_pretty function: This function converts the size in bytes to a more readable format (e.g., MB, GB).

SELECT pg_size_pretty(pg_database_size('your_database_name'));



4. Using \l+ command in psql: This command lists all databases with their sizes.

\l+



5. Using pg_stat_user_tables view: This view provides statistics about user tables, including their sizes.

SELECT relname AS table_name,
       pg_size_pretty(pg_total_relation_size(relid)) AS total_size
FROM pg_catalog.pg_statio_user_tables
ORDER BY pg_total_relation_size(relid) DESC;



These methods provide different levels of detail and can be used depending on your specific needs.

Database
Postgresql
Author
Ankit Shah