我可以放下吗?在执行pg_restore命令时创建数据库? [英] Can I drop & create db while executing pg_restore command?
问题描述
我正在尝试执行以下命令来还原Docker容器中的数据库:
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"
但是它抛出:"pg_restore:错误:连接到数据库"employee"失败:致命:数据库员工"不存在"()(如果我创建了一个具有相同名称的空数据库,则创建该数据库不会有任何问题).
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).
原因可能是:
使用此选项时,以-d命名的数据库仅用于发出初始DROP DATABASE和CREATE DATABASE命令.所有数据都将还原到存档中显示的数据库名称中.
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.
那么,是否可以使用相同的pg_restore命令还原数据库?还是应该通过添加drop并创建命令来解决该问题来对其进行修改?
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?
推荐答案
最后,我得出结论,在还原之前删除并创建了db.如果您有更好的解决方案,欢迎您,我会尝试.
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"
这篇关于我可以放下吗?在执行pg_restore命令时创建数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!