1

I am trying to execute the following command to restore db in a Docker container:

cmd /c "docker exec -i database-container pg_restore -C -U postgres -d employee -v < C:\\backup\\employee.tar"

But it throws: "pg_restore: error: connection to database "employee" failed: FATAL: database "employee" does not exist" (If I create an empty database with the same name, it is created without any problem).

The reason may be:

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.

So, is it possible to restore db using the same pg_restore command? Or should I modify it by adding drop and create commands to fix the problem?

7
  • Indeed ... ideally you'd make the other postgresql-instance to generate a dump file including the "create database" option. How did you create the tarball? Commented Apr 24, 2021 at 0:56
  • I used pgAdmin to create an empty database before executing that command. But it would be better to create it in the same commend or extra commands before executing the restore command. ANy idea? Commented Apr 24, 2021 at 0:59
  • I also tried via cmd /c "docker exec database-container pg_restore --username=postgres --no-password --dbname=employee --verbose C:\backup\employee.tar" but not worked. Commented Apr 24, 2021 at 1:15
  • How did you create the tarball? Commented Apr 24, 2021 at 1:21
  • I dıownloaded it from a site I did not get or take it as backup from db. Commented Apr 24, 2021 at 1:26

2 Answers 2

0

Finally I concluded to drop and create db before restore. If you have a better solution you are welcome and I will give a try.

docker exec database-container bash -c "dropdb -U postgres employee"
docker exec database-container bash -c "createdb -U postgres employee"

cmd /c "docker exec -i database-container pg_restore -C -U postgres -d employee -v 
    < C:\\backup\\employee.tar"
Sign up to request clarification or add additional context in comments.

Comments

0

You can let pg_restore drop and recreate the database before restoring your backup by providing both parameters --create --clean together.

--create Create the database before restoring into it. If --clean is also specified, drop and recreate the target database before connecting to it.

See https://www.postgresql.org/docs/12/app-pgrestore.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.