Techtrekking

How to get table and number of rows for any database

By Pravin

Here is simple query that can give you overview of database table and number of rows.

SELECT schemaname AS schema_name, relname AS table_name, n_live_tup AS row_count FROM pg_stat_user_tables ORDER BY schemaname, row_count DESC;

If you want to get size of table, please use below query

SELECT schemaname || '.' || relname AS table_name, pg_size_pretty(pg_total_relation_size(schemaname || '.' || relname)) AS total_size, pg_total_relation_size(schemaname || '.' || relname) / 1024 / 1024 AS size_mb FROM pg_stat_user_tables ORDER BY pg_total_relation_size(schemaname || '.' || relname) DESC;
Comments
No comments yet. Be the first to comment!
Leave a Comment
Your comment will be visible after approval.