can't connect remote server db2 command line - db2

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'

Related

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" ...

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.

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.

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

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"

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.