mirror of
https://github.com/jbranchaud/til
synced 2026-01-07 17:18:02 +00:00
Add Dump And Restore A Database as a postgres til
This commit is contained in:
@@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really
|
|||||||
warrant a full blog post. These are mostly things I learn by pairing with
|
warrant a full blog post. These are mostly things I learn by pairing with
|
||||||
smart people at [Hashrocket](http://hashrocket.com/).
|
smart people at [Hashrocket](http://hashrocket.com/).
|
||||||
|
|
||||||
_411 TILs and counting..._
|
_412 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -190,6 +190,7 @@ _411 TILs and counting..._
|
|||||||
- [Day Of Week For A Date](postgres/day-of-week-for-a-date.md)
|
- [Day Of Week For A Date](postgres/day-of-week-for-a-date.md)
|
||||||
- [Default Schema](postgres/default-schema.md)
|
- [Default Schema](postgres/default-schema.md)
|
||||||
- [Defining Arrays](postgres/defining-arrays.md)
|
- [Defining Arrays](postgres/defining-arrays.md)
|
||||||
|
- [Dump And Restore A Database](postgres/dump-and-restore-a-database.md)
|
||||||
- [Edit Existing Functions](postgres/edit-existing-functions.md)
|
- [Edit Existing Functions](postgres/edit-existing-functions.md)
|
||||||
- [Escaping A Quote In A String](postgres/escaping-a-quote-in-a-string.md)
|
- [Escaping A Quote In A String](postgres/escaping-a-quote-in-a-string.md)
|
||||||
- [Export Query Results To A CSV](postgres/export-query-results-to-a-csv.md)
|
- [Export Query Results To A CSV](postgres/export-query-results-to-a-csv.md)
|
||||||
|
|||||||
31
postgres/dump-and-restore-a-database.md
Normal file
31
postgres/dump-and-restore-a-database.md
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
# Dump And Restore A Database
|
||||||
|
|
||||||
|
PostgreSQL comes with two command-line utilities for dumping and then
|
||||||
|
restoring a database -- `pg_dump` and `pg_restore`, respectively.
|
||||||
|
|
||||||
|
Using the `pg_dump` with the `-Fc` flag will create a dump of the given
|
||||||
|
database in a custom format. The output of this command can be redirected
|
||||||
|
into a file (the `.dump` extension is a standard convention):
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ pg_dump -Fc my_database > my_database.dump
|
||||||
|
```
|
||||||
|
|
||||||
|
Using the custom format option provides a couple benefits. The output is
|
||||||
|
significantly compressed in comparison to a generic SQL dump. The dump and
|
||||||
|
restoration is more flexible. Lastly, the dump can be performed in parallel
|
||||||
|
if your machine has multiple cores to work with. Likewise, the restoration
|
||||||
|
can be done in parallel with multiple jobs.
|
||||||
|
|
||||||
|
To restore the dump, create a fresh database and then use `pg_restore`:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ createdb my_new_database
|
||||||
|
$ pg_restore -d my_new_database my_database.dump
|
||||||
|
```
|
||||||
|
|
||||||
|
See the
|
||||||
|
[`pg_dump` docs](http://www.postgresql.org/docs/current/static/app-pgdump.html)
|
||||||
|
and [`pg_restore`
|
||||||
|
docs](http://www.postgresql.org/docs/current/static/app-pgrestore.html)
|
||||||
|
for more details.
|
||||||
Reference in New Issue
Block a user