How can I create and drop a postgres database using Liquibase? - postgresql

How can I create a postgres database using Liquibase?
I want to execute the following command by Liquibase:
create database db_name owner user_name;
How can I create and drop a postgres database using Liquibase?
I want to execute the following command by Liquibase:
drop database db_name;

I can create this using
create own bat file
read liquibase.property file from it
create Db
run liquibase.jar update
echo off
FOR /F "eol=; tokens=2,2 delims==" %%i IN ('findstr "url" liquibase.properties') DO set url=%%i
echo %url%
FOR /f " tokens=3,3 delims=/" %%i IN ("%url%") DO set url2=%%i
echo %url2%
Echo "Starting Deployment" >> Deploy.log
Echo "Create Database"
psql.exe -h %1 -p %2 -U %3 -c "create database %url2%" >> Deploy.log
Echo "Call liquibase script"
start liquibase.jar update>> Deploy.log
Echo "Liquibase Script End"

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.

can't connect remote server db2 command line

i am try to connect another server using db2 command CONNECT TO 192.168.131.16db2admin:25000/SAMPLE USER db2admin USING Password#123 but remote server not connect Error Display on SQL0104N An unexpected token "192.168.131.16db2admin:25000/SAMPLE" was found
following "TO". Expected tokens may include: "".
SQLSTATE=42601..please give any solution
If you don't want to catalog the remote database, you may construct the configuration file on the fly to make it work with the Db2 command line processor.
The script constructs temporary configuration file and uses the corresponding environment variable with this file name saving its previous value and reverting it back after the db2 connect command.
Linux & Unix example
#!/bin/sh
if [ $# -eq 0 ]; then
echo "Usage: . ./db2connect host:port/dbname USER username [USING password]" >&2
exit 1
fi
DSN=${1}
CFGFILE=./db2dsdriver.cfg.$$
dbname=${DSN#*/}
hp=${DSN%/*}
host=${hp%:*}
port=${hp#*:}
cat > ${CFGFILE} <<EOF
<configuration>
<dsncollection>
<dsn alias="${dbname}" name="${dbname}" host="${host}" port="${port}"/>
</dsncollection>
<databases>
<database name="${dbname}" host="${host}" port="${port}"/>
</databases>
</configuration>
EOF
cfg_bkp=${DB2DSDRIVER_CFG_PATH}
export DB2DSDRIVER_CFG_PATH=${CFGFILE}
shift
db2 connect to ${dbname} "$#"
export DB2DSDRIVER_CFG_PATH=${cfg_bkp}
rm -f ${CFGFILE}
Usage:
. ./db2connect 192.168.131.16:25000/SAMPLE USER db2admin USING Password#123
Don't remove space between the 1-st dot and the path to this executable file.
Windows example:
#echo off
if [%1] == [] (
echo Usage: %~nx0 host:port/dbname USER username [USING password]
goto :eof
)
set cfg_bkp=%DB2DSDRIVER_CFG_PATH%
set CFGFILE=.\db2dsdriver.cfg.tmp
set DB2DSDRIVER_CFG_PATH=%CFGFILE%
for /F "tokens=1-3 delims=:/" %%i in ('echo %1') do (
set host=%%i
set port=%%j
set dbname=%%k
)
if exist %CFGFILE% del %CFGFILE%
db2cli writecfg add -database %dbname% -host %host% -port %port% > nul
db2cli writecfg add -dsn %dbname% -database %dbname% -host %host% -port %port% > nul
db2 terminate
db2 connect to %dbname% %2 %3 %4 %5
set DB2DSDRIVER_CFG_PATH=%cfg_bkp%
del %CFGFILE%
Usage:
db2connect.bat 192.168.131.16:25000/SAMPLE USER db2admin USING 'Password#123'

PostgreSQL: Automated Backup in Windows

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.

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

Postgres command line install from zip file fails to properly start the windows service after the database has been configured

Below is the batch file I am kicking off at the end of an installer to properly configure my postgres database, however there comes an issue wherein the service is unable to be started.
This is what the service parameters look like:
C:\Program Files\pgsql\bin/pg_ctl.exe runservice -N "MyPostGres" -D "C:/Program Files/pgsql/PGDATA"
Batch file
#echo off
if "%1" == "" goto displayUsage
if "%2" == "" goto displayUsage
if "%3" == "" goto displayUsage
goto setup
:displayUsage
echo supply arguments: SUPER_USER_NAME SUPER_USER_PASSWORD USER_PASSWORD \ >> C:\myLog.log
goto end
:setup
echo %~1 >> C:\myLog.log
echo %~2 >> C:\myLog.log
echo %~3 >> C:\myLog.log
echo calling initdb! >> C:\myLog.log
initdb -U MY_PG ../PGDATA
echo calling pg_ctl register! >> C:\myLog.log
rem provide a variable for PGDATA
pg_ctl register -N TravisPostGres -U MY_PG -P wooo! -D "C:\Program Files\pgsql\PGDATA"
echo calling pg_ctl start! >> C:\myLog.log
pg_ctl start -D ../PGDATA -w
echo calling createuser! %~1 >> C:\myLog.log
createuser -s -UMY_PG %~1
:end
echo the end! >> C:\myLog.log
exit /B 0
I discovered that the issue was the existence of the following file
C:\Program
This file is invisible via explorer view but when executing dir from the c directory it was shown as a 0k file. Deleting this file proved to be the fix.