PostgreSQL - Size of a Database
Last Updated :
15 Apr, 2025
Efficient database management is essential for ensuring optimal performance in PostgreSQL. One critical aspect of this is monitoring the database size to manage storage and plan for scaling. PostgreSQL offers powerful built-in functions like pg_database_size
()
to calculate the size of a specific database, and pg_size_pretty
()
to format the result in human-readable formats such as KB, MB, or GB.
In this article, we will explain the Size of a PostgreSQL Database, how to find the size of a database in PostgreSQL, explore examples for querying individual and multiple databases, and learn how to interpret the results effectively.
Why Monitor Database Size in PostgreSQL?
Monitoring PostgreSQL database size is essential to:
- Optimize performance by identifying storage bottlenecks.
- Plan scaling strategies for growing datasets.
- Manage disk space usage and avoid unexpected system failures.
Functions for Calculating PostgreSQL Database Size
PostgreSQL provides several functions to retrieve database sizes:
pg_database_size('database_name')
:
Returns the size of the specified database in bytes.
pg_size_pretty(bytes)
:
Converts the size in bytes to a more readable format (KB, MB, GB).
pg_total_relation_size('relation_name')
:
Calculates the size of a specific table, including indexes.
Syntax
SELECT pg_database_size('database_name');
Here, 'database_name' is the name of the database whose size you want to check. The result is returned in bytes, which can be difficult to interpret for larger databases.
Now let's list all the available database on our server and find their sizes in our example using the below command:
\l
Output

Examples of Finding Database Size in PostgreSQL
Let’s explore some practical examples to understand how to use these functions effectively. These examples will help us calculate the size of individual databases, tables, and indexes, offering a comprehensive view of storage usage in PostgreSQL.
Example 1: Querying the Size of a Specific Database
Here we will query for the size of the dvdrental database in our server using the below command:
SELECT pg_database_size('dvdrental');
Output

To make the result readable, one can use the 'pg_size_pretty()'
function. The pg_size_pretty() function takes the result of another function and format it using bytes, kB, MB, GB or TB as required. So the above output can be modified as below:
SELECT pg_size_pretty (
pg_database_size ('dvdrental')
);
Output

Example 2: Querying the Size of Another Database
Here we will query for the size of the zoo database in our server using the below command:
SELECT pg_size_pretty (
pg_database_size ('zoo')
);
Output

Example 3: Querying the Size of the sales2020 Database
Here we will query for the size of the sales2020 database in our server using the below command:
SELECT pg_size_pretty (
pg_database_size ('sales2020')
);
Output

Example 4: Querying the Size of All Databases on the Server
Here we will query the size of every database in our current server using the below command:
SELECT
pg_database.datname,
pg_size_pretty(pg_database_size(pg_database.datname)) AS size
FROM pg_database;
Output

This query lists all databases along with their sizes in a readable format. It’s a convenient way to get an overview of storage usage across our entire PostgreSQL instance.
Conclusion
Monitoring PostgreSQL database size is crucial for maintaining database performance and managing storage resources effectively. PostgreSQL provides built-in functions like pg_database_size
()
and pg_size_pretty
()
to simplify the process of calculating and interpreting database sizes.
By using these functions, database administrators can gain insights into storage usage, optimize performance, and plan for scaling effectively. Whether we are managing a single database or an entire server, understanding these tools is essential for efficient database management.
Similar Reads
PostgreSQL Tutorial In this PostgreSQL tutorial youâll learn the basic data types(Boolean, char, text, time, int etc.), Querying and Filtering techniques like select, where, in, order by, etc. managing and modifying the tables in PostgreSQL. Weâll cover all the basic to advance concepts of PostgreSQL in this tutorial.
8 min read
PostgreSQL DATEDIFF Function PostgreSQL doesnât have a DATEDIFF function like some other databases, but you can still calculate the difference between dates using simple subtraction. This approach allows you to find out how many days, months, or years separate two dates. In this article, we'll explore how to compute date differ
6 min read
PostgreSQL - Data Types PostgreSQL is a powerful, open-source relational database management system that supports a wide variety of data types. These data types are essential for defining the nature of the data stored in a database column. which allows developers to define, store, and manipulate data in a way that aligns w
5 min read
PostgreSQL - Psql commands PostgreSQL, or Postgres, is an object-relational database management system that utilizes the SQL language. PSQL is a powerful interactive terminal for working with the PostgreSQL database. It enables users to execute queries efficiently and manage databases effectively.Here, we highlight some of th
2 min read
Top 50 PostgreSQL Interview Questions and Answers Are you preparing for a PostgreSQL interview? PostgreSQL is a powerful open-source relational database management system (RDBMS) that is well-known for its reliability, scalability, and rich set of features. Itâs a favorite among developers and businesses alike, making it essential to master if we w
15+ min read
PostgreSQL - Create Database Creating a database in PostgreSQL is an important task for developers and database administrators to manage data effectively. PostgreSQL provides multiple ways to create a database, catering to different user preferences, whether through the command-line interface or using a graphical interface like
5 min read
How to Dump and Restore PostgreSQL Database? PostgreSQL remains among the most efficient and widely applied open-source relational database management systems. It provides the superior function of saving, configuring, and extracting information most effectively. In the process of migrating data, creating backups, or transferring databases betw
6 min read
PostgreSQL - SERIAL When working with PostgreSQL, we need to create tables with unique primary keys. PostgreSQL offers a powerful feature known as the SERIAL pseudo-type which simplifies generating auto-incrementing sequences for columns. In this article, weâll learn about the PostgreSQL SERIAL pseudo-type by explain h
5 min read
PostgreSQL - DISTINCT ON expression The DISTINCT ON clause in PostgreSQL allows us to retrieve unique rows based on specific columns by offering more flexibility than the standard DISTINCT clause. DISTINCT ON allow us to specify which row to keep for each unique value based on an ORDER BY clause. This is particularly useful for select
5 min read
PostgreSQL Connection String A connection string is an essential component that enables applications to communicate with databases or other data sources by providing the necessary configuration details. It consolidates critical information such as the server address, database name, user credentials, and additional parameters li
4 min read