Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
PostgreSQL - Size of Indexes
Next article icon

PostgreSQL – Size of a Database

Last Updated : 15 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

  1. pg_database_size('database_name'):
    Returns the size of the specified database in bytes.
  2. pg_size_pretty(bytes):
    Converts the size in bytes to a more readable format (KB, MB, GB).
  3. 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

Querying the Size of All Databases on the Server

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.



Next Article
PostgreSQL - Size of Indexes

R

RajuKumar19
Improve
Article Tags :
  • Databases
  • PostgreSQL
  • PostgreSQL-function

Similar Reads

  • PostgreSQL - Loading a Database
    In this article we will look into the process of loading a PostgreSQL database into the PostgreSQL database server. Before moving forward we just need to make sure of two things: PostgreSQL database server is installed on your system. A sample database. For the purpose of this article, we will be us
    3 min read
  • PostgreSQL - Show Databases
    In PostgreSQL, viewing a list of all databases on a server requires specific commands, as it doesn’t support a direct SHOW DATABASES statement like MySQL. Instead, you can use the \l or \l+ commands in psql or query the pg_database view to display all databases. In this article, we will guide us thr
    3 min read
  • PostgreSQL - Size of Indexes
    In PostgreSQL, index management is essential for optimizing query performance and ensuring efficient database storage. One important function for assessing the storage requirements of table indexes is the pg_indexes_size() function. In this article, we will explain the pg_indexes_size() function, it
    4 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
  • PostgreSQL - Backup Database
    All commercial firms and corporations are never 100% free of data threats. Being wary of scenarios like data corruption, host or network failures or even deliberate storage facility destruction is extremely important. Therefore, backing up data is a critical activity that every firm depends on to en
    12 min read
  • PostgreSQL - Copy Database
    Copying a PostgreSQL database is essential for tasks like testing, backup, and database migration. PostgreSQL offers efficient tools for duplicating databases either within the same server or across different servers, ensuring data consistency and integrity. This guide explains step-by-step methods
    4 min read
  • PostgreSQL - Size of a Table
    PostgreSQL provides a variety of functions to help you query the size of your tables. We'll focus on the 'pg_relation_size()' function to get the size of a table and enhance the readability of the output using the 'pg_size_pretty()' function. In this article, we will be using a sample database for r
    3 min read
  • How to Setup a PostgreSQL Database Cluster
    A PostgreSQL database cluster refers to a collection of databases managed by a single instance of the PostgreSQL server. Setting up a PostgreSQL cluster is an essential task for organizing multiple databases and achieving high availability, scalability, and load balancing. Whether we are working wit
    5 min read
  • How to use PostgreSQL Database in Django?
    This article revolves around how can you change your default Django SQLite-server to PostgreSQL. PostgreSQL and SQLite are the most widely used RDBMS relational database management systems. They are both open-source and free. There are some major differences that you should be consider when you are
    2 min read
  • PostgreSQL - Size of value
    The size of a value in a database table refers to the space required to store that specific value. Accurately gauging the storage requirements for different data types can be crucial for database optimization and performance. In this article, we will explore the 'pg_column_size()' function in Postgr
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences