A lightweight, opinionated set of scripts and instructions to automate PostgreSQL backups on Windows, verify the backup contents, and log results. This repository contains a timestamped custom-format dump workflow, an automated verification script, and instructions for scheduling with Windows Task Scheduler.
Important: This project targets PostgreSQL installed on Windows (pg_dump/pg_restore/psql in "C:\Program Files\PostgreSQL<version>\bin"). Do NOT commit any secrets (passwords or pgpass files) to this repository.
Status: Stable — tested on PostgreSQL 16 on Windows 10/11.
- Repo layout
- Quick start
- Script: verify_backup.bat
- How verification works
- Scheduling with Task Scheduler
- Restore examples
- Security notes
- Troubleshooting
- License
/ (root)
├── README.md
├── verify_backup.bat # main automation script (Windows batch)
├── backup_flowchart.png # optional visuals
├── postgres-backup-guide.qmd # Quarto blog post source (optional)
└── samples/
└── dump_contents.txt # example output from pg_restore -l
- Clone this repo to a machine that can connect to your PostgreSQL server.
- Edit
verify_backup.batto set paths, DB name, and user. - Create the backup folder (example uses
C:\edb):
md C:\edb- Prepare passwordless authentication for automated runs (see Security notes).
- Test manually:
"C:\Program Files\PostgreSQL\16\bin\pg_dump.exe" -h localhost -p 5432 -U postgres -F c -b -v -f "C:\edb\analyticsscdb.dump" analyticsscdb
"C:\Program Files\PostgreSQL\16\bin\pg_restore.exe" -l "C:\edb\analyticsscdb.dump" | more- Save this file as
verify_backup.batin the repo root - Edit the configuration variables at the top to match your environment (PG_BIN, BACKUP_DIR, DB_NAME, DB_USER, DB_HOST).
- The script performs a quantitative check:
- It counts the number of tables in the live DB via a simple SQL query on information_schema.
- It lists the dump contents (
pg_restore -l) and counts "TABLE" entries. - If counts match, the script logs success; otherwise it logs a failure.
- Notes:
- This check is a quick sanity test, not a substitute for a periodic trial restore.
- For full verification, perform a test restore into a temporary database and run smoke tests.
- Open Task Scheduler -> Create Basic Task...
- Name: PostgreSQL Daily Backup (or similar).
- Trigger: Daily (pick a time).
- Action: Start a program -> Program/script: browse to
C:\edb\verify_backup.bat. - Before finishing, check "Open the Properties dialog for this task when I click Finish".
- In Properties:
- General: Select "Run whether user is logged on or not".
- Check "Run with highest privileges".
- Enter your Windows password when prompted.
- (Optional) In Conditions/Settings, configure network and power behavior as needed.
- Restore a custom-format dump and let pg_restore create the DB (if dump includes DB creation):
"C:\Program Files\PostgreSQL\16\bin\pg_restore.exe" -h localhost -p 5432 -U postgres -C -d postgres -v "C:\edb\analyticsscdb_sample.dump"- Restore into an existing DB:
"C:\Program Files\PostgreSQL\16\bin\createdb.exe" -h localhost -p 5432 -U postgres restored_db
"C:\Program Files\PostgreSQL\16\bin\pg_restore.exe" -h localhost -p 5432 -U postgres -d restored_db -v "C:\edb\analyticsscdb_sample.dump"- Restore a plain SQL file:
"C:\Program Files\PostgreSQL\16\bin\psql.exe" -h localhost -p 5432 -U postgres -d restored_db -f "C:\edb\analyticsscdb_sample.sql"- Do NOT check any password files (.pgpass.conf) into version control.
- For automation, create
%APPDATA%\postgresql\pgpass.confwith:and restrict file permissions to your user account only.localhost:5432:*:postgres:your_password_here - Alternatively, use integrated OS credential managers or a secrets vault rather than plaintext passwords.
- If pg_dump fails: ensure PostgreSQL service is running:
netstat -ano | findstr 5432
tasklist /FI "IMAGENAME eq postgres.exe"- If authentication errors: confirm pgpass is present and correctly formatted, or run pg_dump interactively to verify password works.
- If permission denied writing to C:\edb: run the task as Administrator or choose a user-writable folder like
%USERPROFILE%\edb. - Inspect the log:
notepad "C:\edb\backup_log.txt"- Pull requests are welcome for improvements and additional verification checks (e.g., checksums, row counts per table).
- Please do not add any file containing live credentials.
- MIT License — see
LICENSEfile
- Dramane B. Salifou — [email protected] / [email protected] / [email protected]