11

Using Postgres to return the size of a table

SELECT pg_size_pretty(pg_table_size('zlimitreacjed_1')); 

returns '1632 kb'....is there anyway to return this value as MB as opposed to kb's?

jarlh
  • 42,561
  • 8
  • 45
  • 63
John
  • 3,965
  • 21
  • 77
  • 163

1 Answers1

19

Judging by the documentation here, pg_size_pretty automatically adjusts as appropriate. If you want to use megs regardless, you could just use

SELECT pg_table_size('zlimitreacjed_1') / 1024 /1024 || 'MB'; 

pg_table_size returns bytes, so just divide it by 1024 a bunch of times, depending on what you want (once for kb, twice for mb, thrice for gb and so on).

Mihai P.
  • 291
  • 3
  • 11
  • thanks for the reply but this doesnt working SELECT pg_size_pretty(pg_table_size('zzbirmingham_march_1') / 1024 /1024 || 'MB'); fires the error ERROR: function pg_size_pretty(text) does not exist LINE 1: SELECT pg_size_pretty(pg_table_size('zzbirmingham_march_1') ... ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts. ********** Error ********** – John Apr 14 '16 at 09:40
  • @John no, don't use the pg_size_pretty function. Just use the pg_table_size, like I showed you. – Mihai P. Apr 14 '16 at 10:02