Skip to content

Commit

Permalink
doc(psql): add backup / restore instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
coolaj86 committed Oct 13, 2024
1 parent 249880e commit 4d2061b
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions psql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 4d2061b

Please sign in to comment.