From 54ed8d999fe5d106ad480eedf73611c5eb4653a3 Mon Sep 17 00:00:00 2001 From: Liam Pietralla Date: Thu, 16 Oct 2025 08:33:38 +1100 Subject: [PATCH] update commands for postgres --- docs/postgres/dump-and-restore.md | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/docs/postgres/dump-and-restore.md b/docs/postgres/dump-and-restore.md index e28a1dd..b40ddb8 100644 --- a/docs/postgres/dump-and-restore.md +++ b/docs/postgres/dump-and-restore.md @@ -2,20 +2,38 @@ 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: + +``` +:::: +``` + +::: 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 -e PGPASSWORD=mysecretpassword postgres:16 pg_dump -h localhost -U myuser -d mydb -f /tmp/dump.sql +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 -p -U -f /tmp/$(date +%Y%m%d_%H%M%S)__dump.sql -c --if-exists" ``` -## Restoring a Database +## Dumping a Cluster ```bash -docker run --network host --rm -v .:/tmp -e PGPASSWORD=mysecretpassword postgres:16 psql -h localhost -U myuser -d mydb -f /tmp/dump.sql +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 -p -U -f /tmp/$(date +%Y%m%d_%H%M%S)__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 -p -U -f /tmp/dump.sql -X" ``` \ No newline at end of file