Site icon JD Bots

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

Syntax

SELECT pg_size_pretty(pg_database_size('name'));

Both are in the Database Object Size Functions 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=#

CREATE DATABASE notes;

Output

CREATE DATABASE

Then ask for its size:

postgres=#

SELECT pg_size_pretty(pg_database_size('notes'));

Output

 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=#

SELECT pg_database_size('notes');

Output

 pg_database_size
------------------
          7864847
(1 row)

Step 2: List Every Database by Size

postgres=#

SELECT datname, pg_size_pretty(pg_database_size(datname)) AS size FROM pg_database ORDER BY pg_database_size(datname) DESC;

Output

  datname  |  size
-----------+---------
 postgres  | 7839 kB
 template1 | 7839 kB
 template0 | 7681 kB
 notes     | 7681 kB
(4 rows)

Step 3: Or Use \l+ in psql

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

postgres=#

\l+ notes

Output

                                                               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=#

DROP DATABASE notes;

Output

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=#

SELECT pg_size_pretty(pg_database_size(current_database()));

Output

 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

Related

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

Exit mobile version