How to restore postgres 12 dump file into postgres 11.5 - postgresql

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

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.

Postgres pg_restore command to restore complete schema

am using Postgres EnterpriseDB 9.5.0.5
I have taken a schema dump by using the below command
pg_dump -n 'schema1' db1 > schema1.dump
Now i want to restore it in different database (db2) what is the command i have to use.
i tried
pg_restore -d DB2 schema1.dump;
but it is showing error
pg_restore: [archiver] input file does not appear to be a valid archiver
You have two choices:
if you include no -f option for pg_dump, it creates a sql script so then restore using psql, not pg_restore
You could add -fc to create a custom binary/compressed format and then use pg_restore as you are trying to do.
However pg_restore mostly converts the archive to an SQL script, so it is not useful when you start with an sql script.
pg_dump -Fc mydb > db.dump
pg_restore -c -d mydb db.dump

How to restore postgres db from gzip file using psql? (arelle: XBRL SEC DB)

I downloaded the xbrldb_SEC_pg_2014-11-02.pg.gzip postgres pg_dump file from arelle.org. I then ran the schema ddl file in pgAdminIII and it recreated all of the databases, functions, etc.
When I try to restore the databases using the following:
desktop:~/Downloads$ sudo postgres zcat xbrldb_SEC_pg_2014-11-02.pg.gzip | psql -U postgres public
I get:
sudo: postgres: command not found psql: FATAL: Peer authentication failed for user "postgres"
I can zcat the file into a file to expand it. Looks like it is a pg_dump file.
postgres=> pg_restore -a /home/jeremy/Downloads/xbrldb_SEC_pg_2014-11-02.txt
postgres-> ;
ERROR: syntax error at or near "pg_restore"
LINE 1: pg_restore -a /home/jeremy/Downloads/xbrldb_SEC_pg_2014-11-0...
^
postgres=> pg_restore -a postgres /home/jeremy/Downloads/xbrldb_SEC_pg_2014-11-02.txt;
ERROR: syntax error at or near "pg_restore"
LINE 1: pg_restore -a postgres /home/jeremy/Downloads/xbrldb_SEC_pg_...
So then I tried to use PG Admin III, and my output:
/usr/bin/pg_restore --host localhost --port 5432 --username "postgres" --dbname "public" --role "postgres" --no-password --section data --data-only --exit-on-error --table accession --schema public --verbose "/home/jeremy/Downloads/xbrldb_SEC_pg_2014-11-02.backup"
pg_restore: [archiver] input file appears to be a text format dump. Please use psql.
Process returned exit code 1.
May I please ask what I need to do to get the databases restored?
Does anyone know what I need to do to get the database updated from 2014-11-02 to the current date?
You should run psql as postgres user, not zcat, so try to use following:
zcat xbrldb_SEC_pg_2014-11-02.pg.gzip | sudo -u postgres psql public
PS pg_restore is an utility, not a PostgreSQL command, that means you should run it from command line, not from psql.

Postgres restore from .dump file: Invalid byte sequence for encoding "UTF8"

I've downloaded the latest copy of my heroku database with
$ curl -o latest.dump `heroku pg:backups public-url --app myapp`
And this gives a file with the following type on my local machine:
$ file db/latest.dump
db/latest.dump: PostgreSQL custom database dump - v1.12-0
I'm trying to import this into my local Postgres DB (my_db) using psql. However, I'm getting lots of errors:
$ psql my_db < db/latest.dump
...
psql:db/latest.dump:119425: invalid command \?A~??ܣb?{#?+????LS??
psql:db/latest.dump:119426: invalid command \D%!ѡ/F??g????A???dC????|2?M?$?8GTJ??c?E?;??֛Sh??S?[NP?f?2?+H?W????k
... [thousands of lines]
psql:db/latest.dump:261719: ERROR: invalid byte sequence for encoding "UTF8": 0xba
The command psql -f db/latest.dump my_db fails the same way.
What needs to be done in order to import this file locally into a new database with the same schema, etc?
I was able to use pg_restore to solve the problem:
pg_restore --verbose --clean --no-acl --no-owner -h localhost -U myuser -d my_db db/latest.dump