Skip to main content

Windows Pg_Dump Script

Hi ,

I recently developed a pg_dump windows based script, which can help us to schedule the pg_dump through pg_Agent or through windows scheduler.

This script also having the backup retention policy and in any case it retain the last two consistant backups. i.e, if the current backup is successful, then it will remove the 2nd last backup from the backup location .


REM pgAgent Windows Pg_Dump Script 

set BIN="C:\Program Files\PostgreSQL\9.1\bin"
set BACKUPLOC="C:\Dump"
set LOGLOC="C:\Dump"
set USER=%3
set PORT=%2
set DT=%date%
set HOST=%1
%BIN%\psql -Atq -h %HOST% -U %USER% -p %PORT% -c "select translate(replace('%DT%','/','_'),' ','_');" postgres >_TMP
set /p DT=<_TMP

REM Creating Scheduled Backup

%BIN%\psql -Atq -h %HOST% -U %USER% -p %PORT% -c "select replace('%DATABASES%','""','');" postgres > _TMP
%BIN%\psql -Atq -h %HOST% -U %USER% -p %PORT% -c "select 'Pg Dump Start For DB %%D Time :: ' || now();" postgres >> %LOGLOC%\pgDump_%DT%.log
if %DUMP_MODE% EQU CUSTOM ( %BIN%\pg_dump -f %BACKUPLOC%\%HOST%_%%D_%PORT%_pgDump_%DT%.dmp -Fc -b -c -h %HOST% -U %USER% -p %PORT% %%D 2>_Error ) else ( if %DUMP_MODE% EQU TAR ( %BIN%\pg_dump -f %BACKUPLOC%\%HOST%_%%D_%PORT%_pgDump_%DT%.dmp -Ft -b -c -h %HOST% -U %USER% -p %PORT% %%D ) else ( if %DUMP_MODE% EQU PLAIN ( %BIN%\pg_dump -f %BACKUPLOC%\%HOST%_%%D_%PORT%_pgDump_%DT%.dmp -Fp -b -c -h %HOST% -U %USER% -p %PORT% %%D ) else ( exit ) ) ) >> %LOGLOC%\pgDump-%DT%.log 
FOR %%E IN ( _Error ) DO IF %%~zE EQU 0 (  FOR /f "tokens=*" %%F IN ( ' dir  /a /b /O-D %BACKUPLOC%\%HOST%_%%D_%PORT%_pgDump*.dmp^|more +%RETAIN_LAST_BACKUPS% ') DO ( echo %%F >>_Files_To_Delete  )  ) else (
echo Error Occured, Hence Keeping The Last Backups As It Is And Removing This Error Caused Dump ..
del /Q %BACKUPLOC%\%HOST%_%%D_%PORT%_pgDump_%DT%.dmp

%BIN%\psql -Atq -h %HOST% -U %USER% -p %PORT% -c "select 'Pg Dump End For DB %%D Time :: '||now();" postgres >> %LOGLOC%\pgDump-%DT%.log

REM Doing Retention Policy..
FOR /f "tokens=*" %%R IN ( 'type _Files_To_Delete' ) DO (  DEL /Q %BACKUPLOC%\%%R  )
DEL /Q _Files_To_Delete 

How to run
Script_Name <host> <Port> <User> "<DB1>,<DB2>,<DB3>,..."
దినేష్ కుమార్
Dinesh Kumar


  1. Not so far I have found new cool tool to work with postgresql on win - Valentina Studio. Its free edition can do things more than many commercial tools!!
    I very recommend check it.

  2. Good work. You have taken care of many possibilities.


Post a Comment

Popular posts from this blog

Pgpool Configuration & Failback

I would like to share the pgpool configuration, and it's failback mechanism in this post.

Hope it will be helpful to you in creating pgpool and it's failback setup.

Pgpool Installation & Configuration

1. Download the pgpool from below link(Latest version is 3.2.1).

2. Untart the pgpool-II-3.2.1.tar.gz and goto pgpool-II-3.2.1 directory.

3. Install the pgpool by executing the below commands:

./configure ­­prefix=/opt/PostgreSQL92/ ­­--with­-pgsql­-includedir=/opt/PostgreSQL92/include/ --with­-pgsql­-libdir=/opt/PostgreSQL92/lib/ make make install 4. You can see the pgpool files in /opt/PostgreSQL92/bin location.
/opt/PostgreSQL92/bin $ ls clusterdb   droplang  pcp_attach_node  pcp_proc_count pcp_systemdb_info  pg_controldata  pgpool pg_test_fsync pltcl_loadmod  reindexdb createdb    dropuser  pcp_detach_node  pcp_proc_info createlang  ecpg      pcp_node_count   pcp_promote_node oid2name  pcp_pool_status  pcp_stop_pgpool  …

pgBucket - A new concurrent job scheduler

Hi All,

I'm so excited to announce about my first contribution tool for postgresql. I have been working with PostgreSQL from 2011 and I'm really impressed with such a nice database.

I started few projects in last 2 years like pgHawk[A beautiful report generator for Openwatch] , pgOwlt [CUI monitoring. It is still under development, incase you are interested to see what it is, attaching the image here for you ],

pgBucket [Which I'm gonna talk about] and learned a lot and lot about PostgreSQL/Linux internals.

Using pgBucket we can schedule jobs easily and we can also maintain them using it's CLI options. We can update/insert/delete jobs at online. And here is its architecture which gives you a basic idea about how it works.

Yeah, I know there are other good job schedulers available for PostgreSQL. I haven't tested them and not comparing them with this, as I implemented it in my way.
Features are: OS/DB jobsCron style sytaxOnline job modificationsRequired cli options

pgBucket 2.0 Beta Is Ready

I am so glad to announce pgBucket 2.0 beta version, which is evolved from the version 1.0. Below are this version feature highlights and hoping that everybody likes these features.

Event jobs (Cascading jobs)Dedicated configuration fileExtended table printAuto job disableCustom job failureDedicated connection poolerImproved the daemon stability/coding standards Please find the below URL for the features review.