-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
doc(psql): add backup / restore instructions
- Loading branch information
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,7 @@ psql "postgres://[email protected]:5432/db-xxxx?sslmode=require&sslnegoti | |
- .pgpass | ||
- .psqlrc | ||
- How to Import / Export CSV | ||
- How to Backup & Restore | ||
- Session Variables (& Encryption Keys) | ||
|
||
### How to View Rows Vertically | ||
|
@@ -200,6 +201,61 @@ FROM "character_descriptions"; | |
\copy "character_descriptions"("id", "character_name", "description") FROM './character_descriptions.csv' WITH (FORMAT csv, HEADER); | ||
``` | ||
|
||
### How to Backup & Restore | ||
|
||
To backup in a way that will be easy to restore: | ||
|
||
- save the schema separately from the data | ||
- don't include database-specific roles or permissions | ||
- store the password in `~/.pgpass` as described above | ||
|
||
Given these credentials: | ||
|
||
```sh | ||
my_user="db_xxxx" | ||
my_db="db_xxxx" | ||
my_host="pg-1.example.com" | ||
my_port="5432" | ||
``` | ||
|
||
And these `pg_dump` commands: | ||
|
||
```sh | ||
pg_dump --no-privileges --no-owner --schema-only --clean \ | ||
--username "$my_user" --no-password --host "$my_host" --port "$my_port" \ | ||
-f ./"$my_db".schema.drop.sql "$my_db" >&2 | ||
|
||
pg_dump --no-privileges --no-owner --schema-only \ | ||
--username "$my_user" --no-password --host "$my_host" --port "$my_port" \ | ||
-f ./"$my_db".schema.sql "$my_db" >&2 | ||
|
||
pg_dump --no-privileges --no-owner --data-only \ | ||
--username "$my_user" --no-password --host "$my_host" --port "$my_port" \ | ||
-f ./"$my_db".data.sql "$my_db" | ||
``` | ||
|
||
You'll get your data is this format: | ||
|
||
```text | ||
db_xxxx.schema.drop.sql # will replace (DELETE) all tables with empty tables | ||
db_xxxx.schema.sql # will create new empty tables | ||
db_xxxx.data.sql # will load data | ||
``` | ||
|
||
To restore / copy to another database: | ||
|
||
```sh | ||
new_user="db_yyyy" | ||
new_db="db_yyyy" | ||
psql "postgres://$new_user@$my_host:$my_port/$new_db" < ./db_xxxx.schema.sql | ||
psql "postgres://$new_user@$my_host:$my_port/$new_db" < ./db_xxxx.data.sql | ||
``` | ||
|
||
See the examples at: | ||
|
||
- https://github.com/bnnanet/pg-essentials?tab=readme-ov-file#psql-backup | ||
- https://github.com/therootcompany/pg-xzbackup.sh | ||
|
||
### How to use Session Variables | ||
|
||
Given the example `psqlrc` above which creates per-db history and config files, | ||
|