PostgreSQL: Automated Backup in Windows - postgresql

I'm trying to create an automated backup in Postgresql using below link, but I don't know where to find the needed dll, I'm stuck here. Can't proceed to next instruction because of this. Can anyone knows how to do it? Need help please.
https://wiki.postgresql.org/wiki/Automated_Backup_on_Windows
comerr32.dll
gssapi32.dll
k5sprt32.dll
krb_32.dll
libeay32.dll
libiconv2.dll
libpq.dll
Microsoft.VC80.CRT.manifest
msvcm80.dll
msvcp80.dll
msvcr80.dll
pg_dump.dll
ssleay32.dll
zlib1.dll
Here's batch file script:
#echo off
for /f "tokens=1-4 delims=/ " %%i in ("%date%") do (
set dow=%%i
set month=%%j
set day=%%k
set year=%%l
)
set datestr=%month%_%day%_%year%
echo datestr is %datestr%
set BACKUP_FILE=<backup_name_>_%datestr%.backup
echo backup file name is %BACKUP_FILE%
SET PGPASSWORD=<password>
echo on
bin\pg_dump -i -h <localhost> -p 5432 -U <postgres> -F c -b -v -f %BACKUP_FILE% <db_name>
Is there missing syntax?
When manually executed error shows:
bin\pg_dump: illegal option -- i

I execute something like this
pg_dump.exe -h %SERVER% -p 5432 -U postgres -Fc -d %basedatos% -v -f %filebackup%
This variables replace value respective.

Related

Backup Postgresql with .bat in windows

I want to create a postgresql database backup with a .bat file on Windows
I tried with this code:
#echo off
for /f "tokens=1-4 delims=/ " %%i in ("%date%") do (
set dow=%%i
set month=%%j
set day=%%k
set year=%%l
)
set datestr=%month%_%day%_%year%
echo datestr is %datestr%
set BACKUP_FILE=odoo_%datestr%.backup
echo backup file name is %BACKUP_FILE%
SET PGPASSWORD=openpgpwd
echo on
bin\pg_dump -i -h localhost -p 5432 -U openpg -F c -b -v -f %BACKUP_FILE% formation
But I received nothing.
And with this code:
#echo off
SET PG_BIN="C:\Program Files\OpenERP 7.0-20150818\PostgreSQL\bin\pg_dump.exe"
SET PG_HOST=localhost
SET PG_PORT=5432
SET PG_DATABASE=formation
SET PG_USER=openpg
SET PG_PASSWORD=openpgpwd
SET PG_PATCH=D:\odoo
SET FECHAYHORA=%date:/=%-%time:-0,8%
SET FECHAYHORA=%FECHAYHORA::=-%
SET FECHAYHORA=%FECHAYHORA: =0%
SET PG_FILENAME=%PG_PATH%\%PG_DATABASE%-%FECHAYHORA%.sql
%PG_BIN% - i -h %PG_HOST% -p %PG_PORT% -U %PG_USER% %PG_DATABASE% > %PG_FILENAME%
I have the same problem.
I want to use it later in the Windows Task Scheduler.
This answer was provided by the author:
I think this is the solution for all:
#echo Backup database %PG_PATH%%PG_FILENAME%
#echo off
SET PG_BIN="C:\Program Files\PostgreSQL\bin\pg_dump.exe"
SET PG_HOST=localhost
SET PG_PORT=5432
SET PG_DATABASE=motor_8
SET PG_USER=openpg
SET PG_PASSWORD=openpgpwd
SET PG_PATH=C:\OEM
SET FECHAYHORA=%date:/=%-%time:-0,8%
SET FECHAYHORA=%FECHAYHORA::=-%
SET FECHAYHORA=%FECHAYHORA: =0%
SET PG_FILENAME=%PG_PATH%\%PG_DATABASE%_%d%_%t%.sql
%PG_BIN% -h %PG_HOST% -p %PG_PORT% -U %PG_USER% %PG_DATABASE% > %PG_FILENAME%
#echo Backup Taken Complete %PG_PATH%%PG_FILENAME%
It is hardly surprising that your scripts don't work, because they contain typos all over. I don't claim that I found them all, but here is a list of those that I did spot:
First script:
There is no option -i for pg_dump.
Second script:
There is a bogus space in -i, but since that option doesn't exist and will cause an error, it should be removed.
You set PG_PATCH and reference PG_PATH.
The environment variable PG_PASSWORD is not recognized by libpq. Remove the underscore.
You can figure out all this if you read the error messages you undoubtedly get. You should also always include such error messages in questions like this.

Why does pg_restore return "options -d/--dbname and -f/--file cannot be used together"?

The following Windows batch script fails on the line with database restore:
#Echo off
set Path=C:\Program Files (x86)
set Backup_Path=C:\Program Files (x86)
c:
cd \
cd C:\Program Files (x86)\PostgreSQL\9.1\bin
#echo "Wait ..."
setlocal
set PGPASSWORD=1234
psql.exe -U postgres -c "create database Mydata"
"C:\Program Files (x86)\PostgreSQL\9.1\bin\pg_restore.exe" -h localhost -U postgres -d Mydata -f "Mydata.backup"
pause
endlocal
The error is:
pg_restore : -d / - dbname and -f / - file can not be used together
Try " pg_restore --help "
-f is the output filename, not the input file.
The input file does not have any parameter switch.
c:\>pg_restore --help
pg_restore restores a PostgreSQL database from an archive created by pg_dump.
Usage:
pg_restore [OPTION]... [FILE]
General options:
-d, --dbname=NAME connect to database name
-f, --file=FILENAME output file name
-F, --format=c|d|t backup file format (should be automatic)
-l, --list print summarized TOC of the archive
-v, --verbose verbose mode
-V, --version output version information, then exit
-?, --help show this help, then exit
So you need to use:
pg_restore.exe -h localhost -U postgres -d Mydata "Mydata.backup"
More details in the manual: http://www.postgresql.org/docs/current/static/app-pgrestore.html

pgAgent scheduled job failed on Windows

I am trying to set up the step with Batch file path on particular time in pgAgent via pgAdmin. But when I run that it is failing and in Step statistics I got this Output
C:\Windows\system32>C:\postgresql\run.bat
'psql' is not recognized as an internal or external command,
operable program or batch file.
Details:
Postgresql 9.3.5 on local system account (Current User)
pgAdmin 1.18.1
pgAgent via stack builder with Administrator account (Current User)
in run.bat I have only two statement
#echo off
psql -h localhost -p 5433 -U postgres -d test -a -f "test.sql"
I have psql in system path variable and able to access it in cmd. When I run that bat file manually it is executing without fail. But when I given the batch file path (C:\postgresql\run.bat) in pgAgent jobs it is giving that error in statistics.
Is there anything wrong in my configuration? Why it is always going to that C:\Windows\system32>?
Edit:
My run.bat file
#ECHO OFF
SET LBSDatabaseName=Test
SET dbHost=localhost
SET dbPort=5434
SET dbUser=postgres
SET logFile=DbInstall.log
SET sqlFolder="D:\SOURCECODE\archivescripts"
"C:\Program Files (x86)\PostgreSQL\9.3\bin\psql.exe" -h "%dbHost%" -p "%dbPort%" -d "%LBSDatabaseName%" -U "%dbUser%" -L "%logFile%" -q -f "%sqlFolder%\Archive.sql"
My Archive.sql
update "Archive".emp set "FirstName"='Srikanth Dyapa';
For example,
D:\pgAgent_jobs
is the path where psql located.
D:\pgAgent_jobs\scripts\test.sql
is the path in which my test.sql placed.
D:\pgAgent_jobs\scripts\psqlss.bat
is my bat file to execute test.sql
so my bat file will be like below
#echo off
cd /D D:\\pgAgent_jobs
psql -h localhost -p 5432 -U postgres -d db_name -a -f "D:\pgAgent_jobs\scripts\test.sql"
Note : my pg_hba.conf is configured with trust for all hosts that's why am not passing any password in the above psql command

simple script to backup PostgreSQL database [duplicate]

This question already has answers here:
How do I specify a password to 'psql' non-interactively?
(11 answers)
Closed 8 years ago.
Hello I write simple batch script to backup postgeSQL databases, but I find one strange problem whether the pg_dump command can specify a password?
There is batch script:
REM script to backup PostgresSQL databases
#ECHO off
FOR /f "tokens=1-4 delims=/ " %%i IN ("%date%") DO (
SET dow=%%i
SET month=%%j
SET day=%%k
SET year=%%l
)
SET datestr=%month%_%day%_%year%
SET db1=opennms
SET db2=postgres
SET db3=sr_preproduction
REM SET db4=sr_production
ECHO datestr is %datestr%
SET BACKUP_FILE1=D:\%db1%_%datestr%.sql
SET FIlLENAME1=%db1%_%datestr%.sql
SET BACKUP_FILE2=D:\%db2%_%datestr%.sql
SET FIlLENAME2=%db2%_%datestr%.sql
SET BACKUP_FILE3=D:\%db3%_%datestr%.sql
SET FIlLENAME3=%db3%_%datestr%.sql
SET BACKUP_FILE4=D:\%db14%_%datestr%.sql
SET FIlLENAME4=%db4%_%datestr%.sql
ECHO Backup file name is %FIlLENAME1% , %FIlLENAME2% , %FIlLENAME3% , %FIlLENAME4%
ECHO off
pg_dump -U postgres -h localhost -p 5432 %db1% > %BACKUP_FILE1%
pg_dump -U postgres -h localhost -p 5432 %db2% > %BACKUP_FILE2%
pg_dump -U postgres -h localhost -p 5432 %db3% > %BACKUP_FILE3%
REM pg_dump -U postgres -h localhost -p 5432 %db4% > %BACKUP_FILE4%
ECHO DONE !
Please give me advice
Regards Mick
edited to reflect information in comments
There is no option to feed the password form command line. You need to include the -w or --no-password switch and create a file that will contain the password for the connection. It is a text file in the form
hostname:port:database:username:password
By default this file will be readed from %APPDATA%\postgresql\pgpass.conf, but its location can be changed setting the environment variable PGPASSFILE to the path to the file to use.
You can find more information in the product documentation

Calling psql pg_dump command in a batch script

I am trying to call a pg_dump command in a batch file. First I get all the table names and then loop every table and execute pg_dump command. It has to be probably something like that but I get an error as "syntax error":
for %%T in (psql -U postgres -w -d test_db -t -c "SELECT table_name FROM
information_schema.tables WHERE table_schema='public' AND table_type='BASE TABLE'")
do pg_dump -t %%T -U postgres test_db -w -f "C:\Users\mtuna\Documents\dumpfiles\%%T.sql"
done;
Any help would be appreciated.
Here is a solution:
#echo off
SET TableListeFile=C:\Users\mtuna\Documents\dumpfiles\database_list.txt
REM Saveing all tables name of database test_db on a temp file: database_list.txt
psql -U postgres -d test_db -t -c "SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_type='BASE TABLE'" -o "%TableListeFile%"
REM Loop on liste tables name:
FOR /F "tokens=*" %%I IN (%TableListeFile%) DO (
REM Dump each table on file
pg_dump -U postgres -h localhost -t %%I test_db > "C:\Users\mtuna\Documents\dumpfiles\%%I.sql"
)
REM Delete temp file
del /Q %TableListeFile%
It will prompt you for password input for every dump. If you don't want to be promted, you can use the Pgpass File.
Hope that helps.
Houari.
Backup: batch_file_backup.bat
#echo off
SET PGPATH="E:\PostgreSQL\9.5\bin\pg_dump.exe"
SET PGPASSWORD=admin
%PGPATH% -h 127.0.0.1 -p 5432 -U postgres -F c -b -v -f C:\Users\Pukar\Downloads\backupfile\2017-04-04.backup database_name
Bat File Backup Run PHP Code:
$batchfile_path = "E:/xampp/htdocs/yig2016/ybase/main_app/bizlayer/protected/batch_file_backup.bat";
$WshShell = new COM("WScript.Shell");
$exec = $WshShell->Run($batchfile_path, 0, false);
Restore:batch_file_restore.bat
#echo off
SET PGPATH="E:\PostgreSQL\9.5\bin\pg_restore.exe"
SET PGPASSWORD=admin
%PGPATH% -h 127.0.0.1 -p 5432 -U postgres -d database_name -v C:\Users\Pukar\Downloads\backupfile\2017-04-04.backup
Bat File Restore Run PHP Code:
$batchfile_path =
E:/xampp/htdocs/yig2016/ybase/main_app/bizlayer/protected/batch_file_restore.bat";
$WshShell = new COM("WScript.Shell");
$exec = $WshShell->Run($batchfile_path, 0, false);
References: http://www.somelesson.blogspot.com/2017/04/postgresql-backup-and-restore.html