Setting up Postgres server on local machine - Windows 10 - postgresql

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

Related

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'

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.

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

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!

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