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

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

Related

PostgreSQL pg_basebackup missing toc.dat header file

I'm using the following command to backup my database (PostgreSQL 11.8):
pg_basebackup -D "C:\\temp" -F tar -X f -z -P -U myUser
And the following to restore:
I manually unpack the base.tar.gz => base.tar
pg_restore -h localhost -W -U myUser -c -C -d myDatabase -F tar -v "C:\\temp\\base.tar"
This results in the following error:
pg_restore: [tar archiver] could not find header for file "toc.dat" in tar archive
What am I doing wrong?
Also, I tried different versions of the restore (only data, etc.) but of course the missing header file issue persists.
Thanks for your help!
You cannot use pg_basebackup and pg_restore together:
pg_basebackup is a physical backup tool
pg_restore can only be used with a logical backup created by pg_dump.
There is no single PostgreSQL command to restore a backup created with pg_basebackup.
To restore a physical backup see https://www.postgresql.org/docs/12/continuous-archiving.html#BACKUP-PITR-RECOVERY

How to restore a postgres database from a custom-format dump?

I'm having an issue getting a database restore using pg_restore to work. I've got Postgres 12.2 installed on my Mac running High Sierra (10.13.6). It was installed with Brew.
I created a simple database "foo" with table "foo" to test this out:
Nates-MacBook-Pro:k8s natereed$ psql -d foo
psql (12.2, server 12.1)
Type "help" for help.
foo=# SELECT table_schema,table_name
FROM information_schema.tables
WHERE table_schema='public' ORDER BY table_schema,table_name;
table_schema | table_name
--------------+------------
public | foo
(1 row)
Generate dump:
pg_dump -Fc foo -f foo.backup
When I try to restore, nothing happens. pg_restore just hangs:
pg_restore -h localhost -p 5432 -U postgres -v -Fc -f foo.backup
I've tried various permutations of this command, but every time it just hangs. In Activity Monitor, I see the process but it is not using any CPU.
Online help says:
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
For pg_restore -f is parameter for the output file: it is not the input file. Remove -f in your example:
pg_restore -h localhost -p 5432 -U postgres -v -Fc foo.backup

PSQL COPY can't find file

I am trying to execute this command:
PS C:\Program Files\PostgreSQL\9.4\bin> .\psql.exe -h front.linux-pgsql01.qa.local -p 5432 -d site -U qa -w -c "Delete from product_factor_lolek; COPY product_factor_lolek FROM E'C:\\OP_data\\SEARCH\\1.csv' delimiter '^' CSV;"
My file is located on this path: C:\OP_data\SEARCH\1.csv. But in fact, I've got an error:
ERROR: could not open file "C:\OP_data\SEARCH\1.csv" for reading: No such file or directory
I am using Windows server, PostgreSQL 9.4. What should I write for correct path?
P.S. I can't use \COPY
The COPY command will attempt to access your CSV file on Server (front.linux-pgsql01.qa.local), not in the client. So you must send your CSV to there and point the command to its path or use stdin as you mentioned:
psql.exe -h front.linux-pgsql01.qa.local -p 5432 -d site -U qa -w -c "Delete from product_factor_lolek; COPY product_factor_lolek FROM STDIN' delimiter '^' CSV;" < C:\OP_data\SEARCH\1.csv

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.

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