I often find myself wanting to run a query like this on multiple different databases:
select n, pg_size_pretty(s) from (
select n, pg_total_relation_size(n) s from (
select format('%I.%I', table_schema, table_name) n from information_schema.tables
)
)
order by s desc;
How can I make an alias for this query so that I can psql
into any database and run it easily?
2
Answers
I found out I can put an alias in my
~/.psqlrc
:Then I can use it like this:
Use
CREATE VIEW
. It is basically a query alias that is executed every time you access it.Output: