Services Spotlight:

Order PostgreSQL Tables in a Database by Disk Usage

Written By Corlew Solutions
Updated December 10, 2016
Published December 10, 2016
Why Corlew Solutions?

We write great software and provide amazing technical support. Contact us today if you need help with a software project:

  • Website: Send an inquiry
  • Email:
  • Phone: (703) 688-3058

Article Technology Info

This article discusses the following technologies:

  • PostgreSQL -

Ever wanted to figure out which tables in your PostreSQL database are taking up the most space on the disk? It’s pretty easy to find out if you know the magic query.

First, connect to psql in the terminal via a command like:

1
psql -U postgres

Next, connect to the database of interest:

1
\connect my_database_name_here

Paste the following SELECT statement into terminal and press Enter:

1
2
3
4
5
6
7
8
9
SELECT nspname || '.' || relname AS "relation",
  pg_size_pretty(pg_total_relation_size(C.oid)) AS "total_size"
FROM pg_class C
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
WHERE nspname NOT IN ('pg_catalog', 'information_schema')
AND C.relkind <> 'i'
AND nspname !~ '^pg_toast'
ORDER BY pg_total_relation_size(C.oid) DESC
LIMIT 20;

We hope you found this article useful. If you see any mistakes, missing features or ways to improve it, please let us know in the comments below so we can update its contents. If you're willing to link to us, we would sincerely appreciate it!

Corlew Solutions is a Web Design and Web Application Development company based in Fairfax Virginia, minutes away Washington D.C. If you're looking for great web design from the Northern Virginia area or web design from the Washington D.C. area we can help. Contact Us today!

comments powered by Disqus