I want to loop through each table in my database, and print it's row count.
I can get all table names, but I do not know how to loop through them, assigning SELECT COUNT(*)
to its table.
-- Get all tablenames
SELECT relname
FROM pg_stat_user_tables
Example Result
table_name row_count
------------- -------------
users 321
questions 807
comments 3,145
tags 58
...
Note:
I know you can use pg_stat_user_tables
to get every table's row counts, but I want to see a "hello world" Postgres loop that SELECTS values. I couldn't find any other SO question or example online that was this simplified.