Skip to main content
0 reactions

How to Check the Size of a Database in PostgreSQL

Card for How to Check the Size of a Database in PostgreSQL, with the steps pg_database_size(), pg_size_pretty(), Empty: 7681 kB and \l+ shows Size.
How to Check the Size of a Database in PostgreSQL.

How big is your database? PostgreSQL can tell you with one query. Here’s how to check the size of a database in PostgreSQL, for one database or for all of them at once.

Every command and output on this page is real, from PostgreSQL 19 on macOS.

Syntax

SyntaxSQL
SELECT pg_size_pretty(pg_database_size('name'));
  • pg_database_size('name'): the disk space the database uses, in bytes. The name goes in single quotes, because here it’s a piece of text.
  • pg_size_pretty(…): turns a number of bytes into something easy to read, such as 7681 kB.

Both are in the Database Object Size Functions (opens in a new tab) of the PostgreSQL documentation.

Step 1: Check the Size of One Database

Connect to your server with psql as the postgres user, as Part 2 of the PostgreSQL Tutorial shows. psql connects you to the postgres database, and its prompt is postgres=#.

This guide uses a new, empty database called notes. To follow along, create it first:

postgres=#SQL
CREATE DATABASE notes;
OutputPlain text
CREATE DATABASE

Then ask for its size:

postgres=#SQL
SELECT pg_size_pretty(pg_database_size('notes'));
OutputPlain text
 pg_size_pretty
----------------
 7681 kB
(1 row)

Even an empty database takes about 7.5 MB, because it starts as a copy of a template database with PostgreSQL’s own system tables in it.

Leave out pg_size_pretty to get the exact number of bytes:

postgres=#SQL
SELECT pg_database_size('notes');
OutputPlain text
 pg_database_size
------------------
          7864847
(1 row)

Step 2: List Every Database by Size

postgres=#SQL
SELECT datname, pg_size_pretty(pg_database_size(datname)) AS size FROM pg_database ORDER BY pg_database_size(datname) DESC;
OutputPlain text
  datname  |  size
-----------+---------
 postgres  | 7839 kB
 template1 | 7839 kB
 template0 | 7681 kB
 notes     | 7681 kB
(4 rows)
  • pg_database lists every database on the server, and datname is its name column.
  • AS size names the result column.
  • ORDER BY … DESC puts the biggest database first.

Step 3: Or Use \l+ in psql

psql’s \l+ lists databases with a Size column. Add a name to see just one:

postgres=#Plain text
\l+ notes
OutputPlain text
                                                               List of databases
 Name  |  Owner   | Encoding | Locale Provider | Collate | Ctype | Locale | ICU Rules | Access privileges |  Size   | Tablespace | Description
-------+----------+----------+-----------------+---------+-------+--------+-----------+-------------------+---------+------------+-------------
 notes | postgres | UTF8     | libc            | C       | C     |        |           |                   | 7681 kB | pg_default |
(1 row)

How to List Databases in PostgreSQL covers \l and \l+ in full.

Clean Up

To remove the example database:

postgres=#SQL
DROP DATABASE notes;
OutputPlain text
DROP DATABASE

Common Mistakes

MistakeWhat happensDo this instead
Leaving out the quotes: pg_database_size(notes)ERROR: column "notes" does not existPut the name in single quotes: pg_database_size('notes').
Capitals in the name: pg_database_size('Notes')ERROR: database "Notes" does not existType the name exactly as it’s stored, usually in lowercase. Text in quotes isn’t changed to lowercase, as Why PostgreSQL Turns Names Lowercase, and What Double Quotes Do explains.

FAQ

How do I check the size of the database I’m connected to?

Use current_database(), which returns the name of the database you’re connected to:

postgres=#SQL
SELECT pg_size_pretty(pg_database_size(current_database()));
OutputPlain text
 pg_size_pretty
----------------
 7839 kB
(1 row)

Conclusion

Run SELECT pg_size_pretty(pg_database_size('name')); for one database, query pg_database to list them all by size, or use \l+ in psql.

Additional Resources

Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.

0 comments