pg_restore ERROR: “Relation does not exist” and creating new database
-
29-12-2020 - |
Pergunta
I have made a backup of my specific tables that I want to restore into a new database using:
call pg_dump -Fc -h server -d database -U user -p password -v -f dump.sql -t public.table1 -t public.table2
And I have no problems.
I then want to restore the dump by creating a new database with pg_restore using:
call pg_restore --clean --create -d temp -h server -p password -U user dump.sql
This gives me a "database temp does not exist" error. This should have created the database and restored it as far as I understand.
I However then create the "temp" database manually and run:
call pg_restore --clean --create -d temp -h server -p password -U user dump.sql
This follows through, but does not create the tables and gives me an error "relation table1
" and "relation table2
" does not exist and only creates the corresponding id_sewuences for the two tables.
What I actually want is to not have to manually create the new database and that all tables in the backup is restored into a brand new database via pg_restore using:
call pg_restore --clean --create -d temp -h server -p password -U user dump.sql
as I understand it.
Please Help, very frustrating
Solução
When --create
and -d
are used together, the argument to -d
is not the name of the database to create, it's the name of an existing database to connect to run the CREATE DATABASE
statement, because it's impossible to create a database if you're not already connect to another database.
This is documented as:
When this option is used, the database named with -d is used only to issue the initial DROP DATABASE and CREATE DATABASE commands. All data is restored into the database name that appears in the archive.
This is why pg_restore
errors out with database temp does not exist
You should add a createdb
step in your restore procedure since you want to create a specific database name anyway, not one that comes from the backup.
For the second problem, it's a completely different question, hard to guess why it happens without complete error messages.
Also note that -p
is not followed by a password but by a port number.