pg_size_pretty()

A function for displaying sizes in bytes in human-readable format

pg_size_pretty() is a system function for displaying a size in bytes into human-readable format.

pg_size_pretty() was added in PostgreSQL 8.1.

Usage

pg_size_pretty ( bigint ) → text
pg_size_pretty ( numeric ) → text

The size will be output with the appropriate size unit: bytes, kB, MB, GB, TB or (from PostgreSQL 15) PB. Note that the units are powers of 2 rather than powers of 10, e.g. 1kB represents 1024 bytes, 1MB represents 1048576 bytes (10242), etc.

Note that as of PostgreSQL 14, sizes of less than 10 times the matching unit will be displayed as the next-lowest unit, e.g.:

postgres=# SELECT pg_size_pretty(1024*9::bigint), pg_size_pretty(10240::bigint);
 pg_size_pretty | pg_size_pretty 
----------------+----------------
 9216 bytes     | 10 kB
(1 row)

Change history

Examples

Basic usage example for pg_size_pretty():

postgres=# SELECT pg_size_pretty(16384::bigint);
 pg_size_pretty 
----------------
 16 kB
(1 row)

Listing database sizes:

postgres=# SELECT datname, pg_size_pretty(pg_database_size(oid)) FROM pg_database ORDER  BY 1;
  datname  | pg_size_pretty 
-----------+----------------
 appdb     | 6137 MB
 postgres  | 7689 kB
 testdb    | 7817 kB
 template0 | 7497 kB
 template1 | 7649 kB
(5 rows)

Negative values can also be specified:

gres=# SELECT pg_size_pretty(-324324321::bigint);
 pg_size_pretty 
----------------
 -309 MB
(1 row)

Categories

System function

See also

pg_size_bytes()