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
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 as7681 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:
CREATE DATABASE notes;
CREATE DATABASE
Then ask for its size:
SELECT pg_size_pretty(pg_database_size('notes'));
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:
SELECT pg_database_size('notes');
pg_database_size
------------------
7864847
(1 row)
Step 2: List Every Database by Size
SELECT datname, pg_size_pretty(pg_database_size(datname)) AS size FROM pg_database ORDER BY pg_database_size(datname) DESC;
datname | size
-----------+---------
postgres | 7839 kB
template1 | 7839 kB
template0 | 7681 kB
notes | 7681 kB
(4 rows)
pg_databaselists every database on the server, anddatnameis its name column.AS sizenames the result column.ORDER BY … DESCputs 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:
\l+ notes
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:
DROP DATABASE notes;
DROP DATABASE
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Leaving out the quotes: pg_database_size(notes) | ERROR: column "notes" does not exist | Put the name in single quotes: pg_database_size('notes'). |
Capitals in the name: pg_database_size('Notes') | ERROR: database "Notes" does not exist | Type 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:
SELECT pg_size_pretty(pg_database_size(current_database()));
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
- PostgreSQL 19 documentation: Database Object Size Functions (opens in a new tab)
- PostgreSQL 19 documentation: psql (opens in a new tab)
Related
- PostgreSQL Tutorial — Part 4: Create a Database
- How to Drop a Database in PostgreSQL
- template0 vs template1 in PostgreSQL
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