diff --git a/README.md b/README.md index 5aabe9f..b6e4449 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1644 TILs and counting..._ +_1645 TILs and counting..._ See some of the other learning resources I work on: - [Get Started with Vimium](https://egghead.io/courses/get-started-with-vimium~3t5f7) @@ -806,6 +806,7 @@ If you've learned something here, support my efforts writing daily TILs by - [Check If The Local Server Is Running](postgres/check-if-the-local-server-is-running.md) - [Check If User Role Exists For Database](postgres/check-if-user-role-exists-for-database.md) - [Check Table For Any Oprhaned Records](postgres/check-table-for-any-orphaned-records.md) +- [Check The Size Of Databases In A Cluster](postgres/check-the-size-of-databases-in-a-cluster.md) - [Checking Inequality](postgres/checking-inequality.md) - [Checking The Type Of A Value](postgres/checking-the-type-of-a-value.md) - [Clear The Screen In psql](postgres/clear-the-screen-in-psql.md) diff --git a/postgres/check-the-size-of-databases-in-a-cluster.md b/postgres/check-the-size-of-databases-in-a-cluster.md new file mode 100644 index 0000000..01217c2 --- /dev/null +++ b/postgres/check-the-size-of-databases-in-a-cluster.md @@ -0,0 +1,39 @@ +# Check The Size Of Databases In A Cluster + +The `\l` command in `psql` will list all the databases for the server. The +field surfaced by this meta-command are: + +- Name +- Owner +- Encoding +- Locale Provider +- Collate +- Ctype +- ICU Locale +- ICU Rules +- Access privileges + +If we add a `+`, issuing instead `\l+`, we get three additional fields: + +- Size +- Tablespace +- Description + +The _Size_ column is the human-formatted size of each database. + +Another way to do this is with some SQL querying the underlying record keeping +of the server's database. + +```sql +select + db.datname as db_name, + pg_size_pretty(pg_database_size(db.datname)) as db_size +from pg_database db +order by pg_database_size(db.datname) desc; +``` + +Credit to [this StackOverflow +answer](https://stackoverflow.com/a/18907188/535590) for how to do this with a +SQL query. + +[source](https://www.postgresql.org/docs/current/app-psql.html#APP-PSQL-META-COMMAND-LIST)