1 min read

Counting table row counts in PostgreSQL

An easy way to count the number of rows in a PostgreSQL table and sort by totals allowing you to find what's taking up space in your database.

Counting table row counts in PostgreSQL
Isaac Bythewood Isaac Bythewood
2022-05-28

I sometimes find myself running into the problem of hunting down what is taking up a lot of rows in PostgreSQL due to service row restrictions. There is a choice of increasing my service plan but I sometimes find it unnecessary if I have a rogue app just adding a lot of data that can be purged. This happens a lot of logs and security apps tracking login attempts. To find the number of rows used in a PostgreSQL database and order it by count you can run this in psql.

If this runs correctly you should see a sorted list of tables with their row counts. From there you can create a script to purge the offending apps on a schedule if you don't need older data.


Some posts in similar tags to this one.

Optimizing SQLite for Django in production
Optimizing SQLite for Django in production
The default SQLite settings in Django are fine for development but will hit "database is locked" errors under any concurrency. Here's the config I use in production.
Isaac Bythewood Isaac Bythewood
2026-04-18
Self-host your fonts
Self-host your fonts
Three reasons every site should self-host fonts in 2026, and why @fontsource makes it a one-liner.
Isaac Bythewood Isaac Bythewood
2026-05-03
Cool URIs don't change, unless an AI rewrites your blog
Cool URIs don't change, unless an AI rewrites your blog
A short post-mortem on letting an AI port my blog from Django to Flask, and the URL design mistake it cheerfully shipped along the way.
Isaac Bythewood Isaac Bythewood
2026-04-29