pg_restore: [compress_io] could not uncompress data: invalid block type - postgresql

I'm getting this error when I restore a Postgres backup from another server:
pg_restore: [compress_io] could not uncompress data: invalid block type
I haven't found much online about what that error means. Except of this discussion.
Is it a problem with the backup file? Is it corrupted? Or is there anything that I need to do in order for the target database to read the block type?
This is part of a script running in crontab for creating the backup: ...
if /usr/pgsql-9.1/bin/pg_dump -Fc -Z 9 -U postgres $PG_DATABASE -p $PG_PORT --schema=$SCHEMA > $PG_BACKUP_FOLDER/notus_dump_$NOW-$SILO.backup 2>/tmp/pg_dump.err
... And this is the command that restores it:
/usr/pgsql-9.1/bin/pg_restore -v -U postgres -p $PG_PORT -C -e -d $PG_DATABASE -n $SCHEMA [backup file]
All variables resolve correctly. The problem is on restore, it starts restoring but it exits with the error in the title.

Related

Postgresql restore with compressed dump file

I took a postgresql DB Bacup with the below command
pg_dump -Z6 -h localhost test_db -f test_db.tar -p port
Now If I try to restore it with psql facinf the below error:
psql:test_db.sql:117359: error: invalid command \gg<CB>t<E4>Iƣ<AC>=<AF>*ESC^TDuV^P^Ov<B6><B0>
psql:test_db.sql:117362: error: invalid command \a>#
psql:test_db.sql:117363: error: invalid command \"<99>k<86>
psql:test_db.sql:117372: error: invalid command \<9B>遥<FD><FE><BA>j
psql:test_db.sql:117406: ERROR: invalid byte sequence for encoding "UTF8": 0x8a
With pg_restore:
pg_restore: error: input file does not appear to be a valid archive
I thought i was using -Fc with pg_dump, but somehow I missed it. Now Can you help me to restore this Backup to postgresql.
I'm Using PostgreSQL 14.5
I've tried below commands
cat test_db.tar | psql test_db -U postgres -h localhost -p port
\i testdb.tar
even Tried to rename the dump file from test_db.tar to test_db.dump and tried restoring it. Nothing is working.
It seems -Z6 generates a gzipped file.
You can convert that into a plain text SQL script that psql can read using. To unzip it using gzip you will need to rename it, e.g. to test_db.sql.gz
Then you can use:
gzip -k -d test_db.sql.gz
-k will keep the original file

Copying local postgresql to AWS RDS

I have a Postgesql database that I want to copy/replicate from my local machine to an AWS RDS instance.
I've created a dump as follows:
pg_dump -v -U postgres mydb > mydb.dump
I then tried to copy to my RDS as follows:
psql -f mydb.dump -h XXXXXXXXXXXX.us-east-1.rds.amazonaws.com -p 5432 -U postgres -d mydb
However, I get the following error:
psql:mydb.dump:1: error: invalid command \
psql:mydb.dump:6: ERROR: syntax error at or near "ÿ_"
LINE 1: ÿ_-"\o""edrp\nou"tnsctme e
^
I then tried to rule out any issues with RDS by copying the archive to another local database as follows:
pg_restore -U postgres -d mydbcopy mydb.dump
I received an error:
pg_restore: error: input file does not appear to be a valid archive
I also tried the preceding as follows:
psql -f my.dump -U postgres -d mydbcopy
And got the same error as before:
psql:mydb.dump:1: error: invalid command \
psql:mydb.dump:6: ERROR: syntax error at or near "ÿ_"
LINE 1: ÿ_-"\o""edrp\nou"tnsctme e
^
I am using Windows Powershell and Postgresql 13.2.
Am I missing something ?
It seems to be an issue with PSQL tools in view that I'm getting the error even when trying to copy locally.
I managed to solve this problem by using
pg_dump -f dump.sql --inserts mydb
It turns out using the redirect ">" means PowerShell does the file writing and it uses UTF16 which doesn't play nice with psql. The -f flag makes pg_dump do the writing itself.

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 postgres 12 dump file into postgres 11.5

dump file generated by pg_dump command in postgres 12
command i use in postgres 11.5:
pg_restore -h [host] -p 5432 -U postgres -d [db] -1 backup.dump
error message:
pg_restore: error: could not set default_table_access_method: ERROR: unrecognized configuration parameter "default_table_access_method"
Is it possible to restore it? please advice.
It seems that only pg_restore throws an error, psql just issues a warning. So you could convert your binary dump to text:
pg_restore dumpfile.Fc -f- | psql -U user -d database
This worked for me on linux.
default_table_access_method is not available in pg11. So the workaround is to take a plain dump of source database and remove the following given below entry from your backup.dump file
SET default_table_access_method = heap;
After that use the below command for restoring the dump on target pg1

pg_restore: using the -L (use-list) option not working

I every now and then restores a db-dump. To save time and I want to exclude some large un-needed tables.
I try to use the -L (use-list) option but when running the pg_restor command it does not work.
I get the same result even if I'm using a un-edited list.
This is the command I use:
pg_restore -v -U [user] -L "restore.pgdump.list" -d [dbname]-h localhost -p 5432 [dump-file]
This is the result:
pg_restore: [archiver] WARNING: line ignored: ÿþ
pg_restore: connecting to database for restore
Password:
pg_restore: implied data-only restore
If I go with the same command without the L-option the database is restored correctly.
Would much appreciate your help
Peter