1 Commits

Author SHA1 Message Date
afee68fa90 Merge pull request 'fix' (#10) from fix/postgres into main
All checks were successful
Build, Test & Publish / Build (push) Successful in 25s
Build, Test & Publish / Build and Publish Container Image (push) Successful in 32s
Build, Test & Publish / Deploy to Infrastructure (push) Successful in 2m31s
Reviewed-on: #10
2025-10-15 16:14:30 +11:00

View File

@@ -2,38 +2,20 @@
Handy commands for dumping and restoring Postgres databases.
::: info
:::info
Most of these commands are run using Docker as then we don't need to have Postgres installed locally.
If a different version of Postgres is needed, just change the version number in the Docker image tag.
:::
## Setup
Create a pgpass file in the current directory with the following content:
```
<server>:<port>:<database>:<username>:<password>
```
::: tip
Use `*` as a wildcard for any of the fields except for the password.
:::
## Dumping a Database
```bash
docker run --network host --rm -v .:/tmp -v ./pgpass:/root/.pgpass -e PGPASSFILE=/root/.pgpass postgres:16 bash -c "chmod 600 /root/.pgpass && pg_dump -h <server> -p <port> -U <username> <database> -f /tmp/$(date +%Y%m%d_%H%M%S)_<database>_dump.sql -c --if-exists"
docker run --network host --rm -v .:/tmp -e PGPASSWORD=mysecretpassword postgres:16 pg_dump -h localhost -U myuser -d mydb -f /tmp/dump.sql
```
## Dumping a Cluster
## Restoring a Database
```bash
docker run --network host --rm -v .:/tmp -v ./pgpass:/root/.pgpass -e PGPASSFILE=/root/.pgpass postgres:16 bash -c "chmod 600 /root/.pgpass && pg_dumpall -h <server> -p <port> -U <username> -f /tmp/$(date +%Y%m%d_%H%M%S)_<server>_cluster_dump.sql -c --if-exists"
```
## Restoring from a SQL Dump
```bash
docker run --network host --rm -v .:/tmp -v ./pgpass:/root/.pgpass -e PGPASSFILE=/root/.pgpass postgres:16 bash -c "chmod 600 /root/.pgpass && psql -h <server> -p <port> -U <username> <database> -f /tmp/dump.sql -X"
docker run --network host --rm -v .:/tmp -e PGPASSWORD=mysecretpassword postgres:16 psql -h localhost -U myuser -d mydb -f /tmp/dump.sql
```