Odoo's backing up database using pg_dump, Odoo's UI command being used - postgresql

I've been studying how to use pg_dump and creating a batch file in order to backup the database of my PostgreSQL since the pgAgent is not appearing on the pgAdmin 3 that Odoo installed on my machine. We all know that there's a method in backing up the database in it's UI at 'http://localhost:8069/web/database/manager'. I was able to backup using pg_dump but it's giving me a file which is 300% the size of backing up the normal way. Is there anyone who knows the command (pg_dump) where i can output the same size and file just as the Odoo's UI does?
Here is my batch file:
cd\
cd "Program Files\Odoo 9.0-20161004\PostgreSQL\bin"
#echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
set dbname=WHpingcon_%fullstamp%
pg_dump -h localhost -p 5432 --format c --file "C:/%dbname%.sql" -U openpg --inserts sample
I used '--inserts' because i also need the whole data of database and restore it the normal way (UI). Everytime i remove '--inserts' it's giving me an error when i'm restoring it. i checked the file and it does not escape special characters. Thanks all!

Related

How to pass a variable to a psql call in a batch file?

I have a batch file that I'm attempting to use to do database backups. In order to try and make this reusable I have parameterised most of the inputs such as:
Database
User
Password
Schema
and so on.
This is what it looks like
#echo on
SET dbPath=%~1
SET dbHost=%~2
SET dbName=%~3
SET dbUser=%~4
SET dbPassword=%~5
SET backupFile=%~6
SET schema=%~7
SET databaseSQL=%~8
SET today=%DATE:~-4%%DATE:~4,2%%DATE:~7,2%
#REM -------------------------------------------------------------------------------------------
:Setup Logging
#REM -------------------------------------------------------------------------------------------
IF NOT EXIST "%dbPath%\logs\" mkdir "%dbPath%\logs"
CD %dbPath%\logs\
IF NOT EXIST restore_log_%schema%_%today%.txt (
type NUL > restore_log_%schema%_%today%.txt
)
SET logFile="%dbPath%\logs\restore_log_%schema%_%today%.txt"
#REM -------------------------------------------------------------------------------------------
:Restore schema
#REM -------------------------------------------------------------------------------------------
echo Started: %date% %time% >> %logFile%
cd ..
SET dbPassword=%dbPassword%
psql -h %dbHost% -p "PASSWORD" -U %dbUser% -d %dbName% -c "DROP SCHEMA IF EXISTS %schema%" >> %logFile%
psql -h %dbHost% -p "PASSWORD" -U %dbUser% -d "dbSUB2" < %backupFile% 2>&1|more >> %logFile%
echo Finished: %date% %time% >> %logFile%
However, when passing in %schema% within double quotes the value of the parameter is converted to lowercase, which is a problem as my tables follow the naming convention of tblTableName. This means the statement says correctly that it cannot find the table as it legitimately doesn't exist.
I have tried using single quotes but that turned out strangely. I also tried the :f flag to just run a separate file but then it did not recognise the variable, the only time it worked was when the file only contained SELECT :variable.
Ideally I'd like to use the first method but I'm unsure how to tell the psql command to not convert my database schema to lowercase.

Setting up Postgres server on local machine - Windows 10

I installed Postgres 14 in local machine(Windows 10), and while installation selected postgres server as well, along with pgadmin and a couple others.
However, im not sure whether postgres server is already started on my local machine.
I tried using psql, but seems the credentials are incorrect. What is the default username ? Why am i unable to connect ? Screenshots below:
Adding batch script below:
#echo off
REM Copyright (c) 2012-2020, EnterpriseDB Corporation. All rights reserved
REM PostgreSQL server psql runner script for Windows
SET server=localhost
SET /P server="Server [%server%]: "
SET database=postgres
SET /P database="Database [%database%]: "
SET port=PG_PORT
SET /P port="Port [%port%]: "
SET username=PG_USERNAME
SET /P username="Username [%username%]: "
for /f "delims=" %%a in ('chcp ^|find /c "932"') do # SET CLIENTENCODING_JP=%%a
if "%CLIENTENCODING_JP%"=="1" SET PGCLIENTENCODING=SJIS
if "%CLIENTENCODING_JP%"=="1" SET /P PGCLIENTENCODING="Client Encoding [%PGCLIENTENCODING%]: "
REM Run psql
"PG_INSTALLDIR\bin\psql.exe" -h %server% -U %username% -d %database% -p %port%
pause
I also get this warning while installing the setup:
Edit 1: Post reinstallation , i am getting an issue which says connection refused
The path to PGSQL is wrong. Either it is saved in the variable PG_INSTALLDIR, which means it should be written between %%
%PG_INSTALLDIR%\bin\psql.exe ...
or it is not and you should write the full path like
"C:\Program Files\PostgreSQL\14\bin\psql.exe" ...

PgAgent job fails running pgdump task from batch file

I'm trying to schedule a batch file to backup a database on postgres. However, the job gets stuck on status "r" with no indication of what's going on, however a 0kb SQL file is created. When I run the batch file manually, it executes perfectly fine. A normal insert statement runs fine
OS: Windows 10
postgres v11.4
pg admin v4.9
I'm running this on the host itself with superuser postgres
Contents of batch file:
#ECHO OFF
#setlocal enableextensions
#cd /d "%~dp0"
SET PGPATH=C:\"Program Files"\PostgreSQL\11\bin\
SET SVPATH=D:\pgsqlbak\
SET PRJDB=xxx
SET DBUSR=postgres
SET HOUR=%time:~0,2%%time:~3,2%%time:~6,2%
SET dtStamp1=0%time:~1,1%%time:~3,2%%time:~6,2%
SET dtStamp2=%time:~0,2%%time:~3,2%%time:~6,2%
FOR /F "TOKENS=2,3,4 DELIMS=/ " %%i IN ('DATE /T') DO SET d=%%k%%j%%i
if "%HOUR:~0,1%" == " " (SET dtStamp=%dtStamp1%) else (SET dtStamp=%dtStamp2%)
SET DBDUMP=%PRJDB%_%d%_%dtStamp%.sql
#ECHO OFF
%PGPATH%pg_dump -h 127.0.0.1 -p 5432 -U postgres %PRJDB% > %SVPATH%%DBDUMP%
pgpass:
127.0.0.1:5432:yyy:postgres:password
127.0.0.1:5432:xxx:postgres:password
127.0.0.1:5432:postgres:postgres:password
localhost:5432:yyy:postgres:password
localhost:5432:xxx:postgres:password
localhost:5432:postgres:postgres:password
Also would like to know how to kill a job that is running apart from restarting server
Any help on this will be greatly appreciated

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.

Postgres dump batch secondary backup directory

I'm running this script to backup a database. It works but I would like to add a secondary backup directory to get the output file in two different directories. Any help is appreciated..)
REM Set paths and database info
set PGPASSWORD='postgre'
set PGuser='postgres'
set PGinstance='e3238s'
set PGdump='F:/Program Files/PostgreSQL/9.5/bin\pg_dump'
set BackupDir='F:/backup'
set BackupName='DB_BK'
REM Build a datetime stamp
set DateTime='%DATE:~-4%_%DATE:~-7,2%_%DATE:~-10,2%_%TIME:~0,2%_%TIME:~3,2% _%TIME:~6,2%'
echo %DateTime%
REM Backup
"%PGdump%" -f "%BackupDir%\%BackupName%%DateTime%.sql" --format plain -U %PGuser% -v %PGinstance%
This is quite unrelated to Postgres, and more related to how you'd output to two files in the same run.
You could use something like this, which in your case, becomes:
REM Backup
"%PGdump%" --format plain -U %PGuser% -v %PGinstance% > "%BackupDir%\%BackupName%%DateTime%.sql" & type "%BackupDir%\%BackupName%%DateTime%.sql" >> "%SecondaryBackupDir%\%BackupName%%DateTime%.sql"