PostgreSQL Tutorial
Data Types
Querying & Filtering Data
Managing Tables
Modifying Data
Conditionals
Control Flow
Transactions & Constraints
Working with JOINS & Schemas
Roles & Permissions
Working with Sets
Subquery & CTEs
User-defined Functions
Important In-Built Functions
PostgreSQL PL/pgSQL
Variables & Constants
Stored Procedures
Working with Triggers
Working with Views & Indexes
Errors & Exception Handling
In PostgreSQL, the size of a database can be checked using SQL functions provided by the database system. Here are a few ways you can determine the size of a database:
Total Size of a Specific Database
This query provides the total size of a specific database:
SELECT pg_size_pretty(pg_database_size('your_database_name')) AS size;
Replace 'your_database_name'
with the name of the database you're interested in.
Size of All Databases
This query provides the sizes of all databases in your PostgreSQL instance:
SELECT datname, pg_size_pretty(pg_database_size(datname)) AS size FROM pg_database;
Size of All Tables in a Database
To get the size of all tables within a particular database:
SELECT schemaname, tablename, pg_size_pretty(pg_total_relation_size(schemaname || '.' || tablename)) AS total_size, pg_size_pretty(pg_relation_size(schemaname || '.' || tablename)) AS data_size, pg_size_pretty(pg_total_relation_size(schemaname || '.' || tablename) - pg_relation_size(schemaname || '.' || tablename)) AS external_size FROM pg_tables WHERE schemaname NOT IN ('pg_catalog', 'information_schema') ORDER BY pg_total_relation_size(schemaname || '.' || tablename) DESC;
pg_total_relation_size()
: returns the total size of a table, including all of its associated auxiliary objects (indexes, toast tables, etc.).pg_relation_size()
: returns the size of the table itself.Size of Indexes
If you're interested in the size of the indexes:
SELECT schemaname, indexname, pg_size_pretty(pg_relation_size(schemaname || '.' || indexname)) AS index_size FROM pg_indexes WHERE schemaname NOT IN ('pg_catalog', 'information_schema') ORDER BY pg_relation_size(schemaname || '.' || indexname) DESC;
Always ensure you have necessary permissions to execute these functions, especially if you're not the database administrator.
PostgreSQL database size query: To find the size of a PostgreSQL database, you can use the following SQL query:
SELECT pg_size_pretty(pg_database_size('your_database_name')) AS database_size;
How to check PostgreSQL database size: You can check the size of a PostgreSQL database using:
SELECT pg_size_pretty(pg_database_size('your_database_name')) AS database_size;
PostgreSQL show database size command:
SELECT pg_size_pretty(pg_database_size('your_database_name')) AS database_size;
List all databases and sizes in PostgreSQL:
SELECT datname, pg_size_pretty(pg_database_size(datname)) AS database_size FROM pg_database;
PostgreSQL get database size in MB/GB:
SELECT pg_size_pretty(pg_database_size('your_database_name')) AS database_size;
Query to find largest tables in PostgreSQL:
SELECT table_name, pg_size_pretty(pg_total_relation_size(table_name)) AS table_size FROM information_schema.tables ORDER BY pg_total_relation_size(table_name) DESC LIMIT 10;
Vacuum PostgreSQL database to reclaim space:
VACUUM FULL;
PostgreSQL analyze table size:
ANALYZE VERBOSE your_table;