Book Image

PostgreSQL 9 Administration Cookbook - Second Edition

Book Image

PostgreSQL 9 Administration Cookbook - Second Edition

Overview of this book

Table of Contents (19 chapters)
PostgreSQL 9 Administration Cookbook Second Edition
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

How much disk space does a database use?


For planning or space monitoring, we often need to know how big the database is.

How to do it…

We can do this in the following ways:

  • Look at the size of the files that make up the database server

  • Run a SQL request to confirm the database size

If you look at the size of the actual files, you'll need to make sure that you include the data directory and all subdirectories, as well as all other directories that contain tablespaces. That can be tricky, and it is also difficult to break out all the different pieces.

The easiest way is to just ask the database a simple query, like this:

SELECT pg_database_size(current_database());

However, this is limited to only the current database. If you want to know the size of all the databases together, then you'll need a query such as the following:

SELECT sum(pg_database_size(datname)) from pg_database;

How it works…

The database server knows which tables it has loaded. It also knows how to calculate the size of each table...