diff --git a/README.md b/README.md index 51788e6..c143a0d 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://tinyletter.com/jbranchaud). -_1066 TILs and counting..._ +_1067 TILs and counting..._ --- @@ -532,6 +532,7 @@ _1066 TILs and counting..._ - [Determine Types Of JSONB Records](postgres/determine-types-of-jsonb-records.md) - [Determining The Age Of Things](postgres/determining-the-age-of-things.md) - [Difference Between Explain And Explain Analyze](postgres/difference-between-explain-and-explain-analyze.md) +- [Dump All Databases To A SQL File](postgres/dump-all-databases-to-a-sql-file.md) - [Dump And Restore A Database](postgres/dump-and-restore-a-database.md) - [Duplicate A Local Database](postgres/duplicate-a-local-database.md) - [Edit Existing Functions](postgres/edit-existing-functions.md) diff --git a/postgres/dump-all-databases-to-a-sql-file.md b/postgres/dump-all-databases-to-a-sql-file.md new file mode 100644 index 0000000..08a1c9e --- /dev/null +++ b/postgres/dump-all-databases-to-a-sql-file.md @@ -0,0 +1,38 @@ +# Dump All Databases To A SQL File + +I recently needed to reinstall my local Postgres installation. I had several +databases with data in that cluster that I wanted to preserve. Before I could +go uninstalling and re-installing Postgres, I needed to dump the entire cluster +of databases. + +The `pg_dumpall` command that installs with Postgres can be used for this. + +```bash +$ pg_dumpall > postgres_13_1_cluster_dump.sql +``` + +The command outputs to stdout a SQL dump of all the databases stored in the +data directory of this Postgres instance. + +I took this a step further and ignored the `template0` and `template1` +directories because I knew those would come with the new install. I did that by +adding the `--exclude-database` flag with a pattern. + +```bash +$ pg_dumpall \ + --exclude-database="template*" \ + > postgres_13_1_cluster_dump.sql +``` + +This data dump can be restored with the new install using: + +```bash +$ psql -f postgres_13_1_cluster_dump.sql postgres +``` + +I wrote more about this process in [Reinstall Postgres with OpenSSL Using +asdf](https://dev.to/jbranchaud/reinstall-postgresql-with-openssl-using-asdf-cmj). + +Also, see `pg_dumpall --help` or the [Postgres +docs](https://www.postgresql.org/docs/current/app-pg-dumpall.html) for more +details.