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

PostgreSQL - Size of a Database

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:

  1. 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.

  2. 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;
    
  3. 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.
  4. 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.

  1. 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;
    
  2. 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;
    
  3. PostgreSQL show database size command:

    SELECT pg_size_pretty(pg_database_size('your_database_name')) AS database_size;
    
  4. List all databases and sizes in PostgreSQL:

    SELECT datname, pg_size_pretty(pg_database_size(datname)) AS database_size
    FROM pg_database;
    
  5. PostgreSQL get database size in MB/GB:

    SELECT pg_size_pretty(pg_database_size('your_database_name')) AS database_size;
    
  6. 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;
    
  7. Vacuum PostgreSQL database to reclaim space:

    VACUUM FULL;
    
  8. PostgreSQL analyze table size:

    ANALYZE VERBOSE your_table;