postgresqlpsqlexistspg-dumppg-restore

pg_restore ERROR: “Relation does not exist” and creating new database


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_sequences 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.


Solution

  • In order to create the database temp, pg_restore needs to be connected to a different database first.

    So it won't do to use -d temp; you must specify an existing database, typically postgres.

    pg_restore will connect to that database, issue CREATE DATABASE temp, connect to temp and proceed to restore the data.