From ac18c20fa8ee10846227b9551ff5ae84605a5bd6 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Thu, 14 May 2020 12:18:54 -0500 Subject: [PATCH] Add Duplicate A Local Database as a postgres til --- README.md | 3 ++- postgres/duplicate-a-local-database.md | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 postgres/duplicate-a-local-database.md diff --git a/README.md b/README.md index eb0a343..f198fac 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ and pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud). -_914 TILs and counting..._ +_915 TILs and counting..._ --- @@ -446,6 +446,7 @@ _914 TILs and counting..._ - [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 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) - [Escaping A Quote In A String](postgres/escaping-a-quote-in-a-string.md) - [Escaping String Literals With Dollar Quoting](postgres/escaping-string-literals-with-dollar-quoting.md) diff --git a/postgres/duplicate-a-local-database.md b/postgres/duplicate-a-local-database.md new file mode 100644 index 0000000..6c23dae --- /dev/null +++ b/postgres/duplicate-a-local-database.md @@ -0,0 +1,16 @@ +# Duplicate A Local Database + +You can quickly create a new database instance that is a duplicate of another +database. If the existing database is local, you don't need to dump and +restore. Instead you can use the `createdb` command that comes with Postgres: + +```bash +$ createdb -O ownername -T originaldb newdb +``` + +This creates a new database called `newdb` using `originaldb` as a template +(`-T`). This will include the entire schema and data of the original database. +The `-O` flag allows you to specify the owner of the database. Since this is +local, you probably want your primary unix user as the owner. + +[source](https://stackoverflow.com/a/6739995/535590)