arquivo de entrada parece ser um despejo de formato de texto. Use psql

95

Eu faço backup usando

pg_dump db_production > postgres_db.dump

e então copio para localhost usando scp.

Agora quando eu importo no meu banco de dados local dá um erro

pg_restore: [archiver] input file appears to be a text format dump. Please use psql.

usando linha commad

pg_restore -d db_development postgres_db.dump
Haseeb Ahmad
fonte

Respostas:

138

Da pg_dumpdocumentação:

Exemplos

Para despejar um banco de dados chamado mydb em um arquivo de script SQL :

$ pg_dump mydb > db.sql

Para recarregar esse script em um banco de dados (recém-criado) denominado newdb :

$ psql -d newdb -f db.sql

Para despejar um banco de dados em um arquivo de formato personalizado:

$ pg_dump -Fc mydb > db.dump

Para despejar um banco de dados em um arquivo em formato de diretório:

$ pg_dump -Fd mydb -f dumpdir

Para recarregar um arquivo em um banco de dados (recém-criado) chamado newdb:

$ pg_restore -d newdb db.dump

Da pg_restoredocumentação:

Exemplos

Suponha que tenhamos despejado um banco de dados chamado mydb em um arquivo de despejo de formato personalizado:

$ pg_dump -Fc mydb > db.dump

Para eliminar o banco de dados e recriá-lo do despejo:

$ dropdb mydb
$ pg_restore -C -d postgres db.dump
Зелёный
fonte
2
when i do pg_restore I get pg_restore: [archiver] input file appears to be a text format dump. Please use psql.
Haseeb Ahmad
Which one I use to restore db ?
Haseeb Ahmad
76
this does not answer the question
dopatraman
1
for a way to restore from a text file checkout serverfault.com/questions/260607/…
chrs
7
IMO this post has a little too much detail which obfuscates the answer. What the author failed to point out is that the pg_dump documentation for the '-F' parameter does not say the default format ('p'/'plain') is suitable for pg_restore. pg_restore requires that pg_dump be used with the 'c', 'd', or 't' formats.
Matt
63

The answer above didn't work for me, this worked:

psql db_development < postgres_db.dump

Uziel Valdez
fonte
Short and sweet. Worked for me also.
Pupil
14

For me when i try to restore from remote host i used

psql -U username -p 5432 -h 10.10.10.1 -d database < db.dump

worked fine. And if not remote just following command worked.

psql -d database < db.dump
Tserenjamts
fonte
9

In order to create a backup using pg_dump that is compatible with pg_restore you must use the --format=custom / -Fc when creating your dump.

From the docs:

Output a custom-format archive suitable for input into pg_restore.

So your pg_dump command might look like:

pg_dump --file /tmp/db.dump --format=custom --host localhost --dbname my-source-database --username my-username --password

And your pg_restore command:

pg_restore --verbose --clean --no-acl --no-owner --host localhost --dbname my-destination-database /tmp/db.dump
Tim Fletcher
fonte
6

For me, It's working like this one. C:\Program Files\PostgreSQL\12\bin> psql -U postgres -p 5432 -d dummy -f C:\Users\Downloads\d2cm_test.sql

RAJNISH YADAV
fonte
2
Simple and helpful answer!
Rax Weber
1
ThankYou @RAJNISH YADAV.
Amit
1

if you use pg_dump with -Fp to backup in plain text format, use following command:

cat db.txt | psql dbname

to copy all data to your database with name dbname

M2E67
fonte
0

If you have a full DB dump:

PGPASSWORD="your_pass" psql -h "your_host" -U "your_user" -d "your_database" -f backup.sql

If you have schemas kept separately, however, that won't work. Then you'll need to disable triggers for data insertion, akin to pg_restore --disable-triggers. You can then use this:

cat database_data_only.gzip | gunzip | PGPASSWORD="your_pass" psql -h "your_host" -U root "your_database" -c 'SET session_replication_role = replica;' -f /dev/stdin

On a side note, it is a very unfortunate downside of postgres, I think. The default way of creating a dump in pg_dump is incompatible with pg_restore. With some additional keys, however, it is. WTF?

VasiliNovikov
fonte