Postgres pg_restore command to restore complete schema - postgresql

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

Related

pg_restore is not working - input file does not appear to be a valid archive

I am doing pg_dump using -
pg_dump -U <username> -h <host> <database> > backup.sql
pg_dump is working fine.
I am trying to do pg_restore doing -
pg_restore -U <username> -h <host> -d <databse> backup.sql
Then it is showing pg_restore: error: input file does not appear to be a valid archive
I have checked many StackOverflow answers about this, but I could not figure out anything. Please help me. Thanks in advance.
Update : As per comments we can not use pg_restore for .sql files. Actually I have an restriction that I must have to restore the database using pg_restore command. Can you please give the pg_dump command using which I can restore that using pg_restore?
You created a plain format dump, which is an SQL file. You have to restore plain-format dumps with psql:
psql -U <username> -h <host> -d <databse> -f backup.sql
pg_restore is used to restore dumps in all other formats. You get dumps in other formats by using the appropriate -F option with pg_dump: for example, -F c produces a custom format dump.
If you want to restore a plain format dump with a client other than psql, you have to create it with the option --inserts.

Postgresql - backup restore on different owner?

My question is similar to this I even vote up it, but unfortunately in my case I have text dump and I can not restore using pg_restore =(
zcat /home/kes/work/projects/bennet/db/bennet.sql.gz | \
pg_restore -h 127.0.0.1 -p 5432 -U bennet --no-owner --role=bennet -d bennet
pg_restore: error: input file appears to be a text format dump. Please use psql.
But psql has no role and no-owner options
Is there a way to restore text dump on different owner?
No. You have two options:
Manually edit the SQL script. This is cumbersom and error-prone.
Restore the complete dump to a new, empty scratch database using psql and dump that with --role and --no-owner as you need.

PostgreSQL: import/restore database dump (pg_dump version 9.6.5) on Windows

I am a newbie and I have a postgresql large dump of type .sql and i want to import it. I am on Windows 10 and haven't been able to find solution.
I tried to restore using pgAdmin3 but it doesn't show .sql file while restoring. I also found few commands and tried them but nothing seems to work.
I also tried loading the datasource in IntelliJ DataGrid but it doesn't show the correct driver during the loading settings.
Can someone help?
First, figure out if the dump was created with pg_dumpall or pg_dump.
Dumps from pg_dumpall start with:
--
-- PostgreSQL database cluster dump
--
If the dump was created with pg_dump, find out if the -C option was used.
If yes, the dump will contain a line with a CREATE DATABASE statement.
To restore, use psql from the DOS box. I assume that psql is on your PATH.
A dump from pg_dumpall or pg_dump -C is restored with
psql -U postgres -d postgres -f dumpfile.sql
A dump from pg_dump without -C is restored with
psql -U postgres -d mydatabase -f dumpfile.sql
where mydatabase should be replaced with the name of the target database into which the dump should be restored.

Postgres - copying a table from a dump file

I have a database dump that I generated with pg_dump, like so:
pg_dump -C remote_db -a --no-owner -t my_table > dump.sql
and I'm looking to copy a single table over from it into my local database, with data only (not schema) and with no ownership settings. I'm familiar with how to do it directly from another db using pg_dump, something like:
pg_dump -C remote_db -a --no-owner -t my_table | psql local_db
But I'm not sure how to replicate the same effect from a file.
I've tried something like:
pg_restore -d local_db -a --no-owner -t my_table dump.sql
But got an error:
pg_restore: [archiver] input file appears to be a text format dump. Please use psql.
I'm not sure how to use psql to achieve the same thing. Help would be appreciated.

Is it possible to restore PostgreSQL data without overwriting existing data?

I have 2 backup files and I want to merge all this data on my database.
I try to use pg_restore but when I use with the second database file I lost the first data set.
Look for my command:
pg_restore -U postgres -c --if-exists -d ravpacheco_db "C:\Users\ravpacheco\xpto1.backup"
I also search all options flags for pg_restore command but I can't find some usefull thing
My problem is with my pg_dump command. If I created a backup file only with data my restore will work
Now I'm using this pg_dump command
pg_dump --column-inserts --data-only --table=<table> <database>
I resolved my problem using this stackoverflow thread