Skip to main content

PostgreSQL backup script Windows Server 2019

 A few days ago I had to create a backup script for my PostgreSQL server running on Windows.

After few minutes the script looks like this:


@echo off

SET DBHOST=localhost
set DBNAME=YOUR_DB_NAME
set DBUSER=YOUR_DB_USERNAME
set PGPASSWORD=YOUR_DB_PASSWORD
:: Path to PostgreSQL pg_dump.exe
set PGDUMP="C:\Program Files\PostgreSQL\14\bin\pg_dump.exe" 
:: Get date in YYYY-MM-DD format
set CURRENTDATE=%date:~6,4%%date:~3,2%%date:~0,2%
set BACKUPDIR=BACKUP_DIRECTORY

%PGDUMP% --host %DBHOST% --port 5432 --format custom --blobs --username %DBUSER% -f "%BACKUPDIR%\%CURRENTDATE%-%DBNAME%.backup" %DBNAME%
 
And it is working now like a charm :)

Comments