简体   繁体   English

如果成功有时导致退出代码为1,我如何可靠地确定pg_restore是否成功?

[英]How do I reliably determine whether pg_restore succeeded, when success sometimes results in an exit code of 1?

When running pg_restore --clean --dbname=my_database backup_file.sql to restore a database dump to an empty database, the restore succeeds, but with the following warning message: 运行pg_restore --clean --dbname=my_database backup_file.sql以将数据库转储还原到空数据库时,还原成功,但出现以下警告消息:

pg_restore: [archiver (db)] Error while PROCESSING TOC:
pg_restore: [archiver (db)] Error from TOC entry 161; 1259 16549 TABLE example_table root
pg_restore: [archiver (db)] could not execute query: ERROR:  table "example_table" does not exist
    Command was: DROP TABLE public.example_table;

WARNING: errors ignored on restore: 1

As the message indicates, the restore succeeded. 如消息所示,恢复成功。 There were errors, but pg_restore claims to have ignored them. 有错误,但pg_restore声称忽略了它们。 I was also able to manually query the database to verify that all data I expected to be in the dump was present in the database after the restore. 我还能够手动查询数据库,以验证在恢复后我预期在转储中的所有数据是否都存在于数据库中。

The problem is the command above exited with a status of 1, not 0. When performing database restores programmatically (as I intend to do when I automate this process), this is problematic, since my script needs to be able to reliably determine whether the restore succeeded or not. 问题是上面的命令退出状态为1,而不是0.当以编程方式执行数据库恢复时(正如我打算在自动执行此过程时那样),这是有问题的,因为我的脚本需要能够可靠地确定是否恢复成功与否。

Is there a way to make pg_restore ignore warnings when determining its exit status? 有没有办法让pg_restore在确定退出状态时忽略警告? Or is there some alternative method to pg_restore I can use that I can get more accurate success/failure information from? 或者是否有一些替代方法pg_restore我可以使用,我可以从中获得更准确的成功/失败信息? How can I restore the database and reliably determine programmatically whether the restore succeeded? 如何还原数据库并可靠地以编程方式确定还原是否成功?

Note that I am currently using PostgreSQL 9.1. 请注意,我目前正在使用PostgreSQL 9.1。

Turns out Postgres doesn't actually know that the error mentioned in the question is relatively harmless; 事实证明,Postgres实际上并不知道问题中提到的错误是相对无害的; that's not why the error is ignored. 这不是错误被忽略的原因。 The reason pg_restore is actually ignoring that error is because pg_restore is configured by default to ignore nearly all errors that occur during the restore process. pg_restore实际上忽略该错误的原因是因为pg_restore默认配置为忽略恢复过程中发生的几乎所有错误。 If you care about the success/failure status of the restore, this probably isn't the behavior you want. 如果您关心还原的成功/失败状态,这可能不是您想要的行为。 Running pg_restore with the --exit-on-error or --single-transaction options will remedy this, but will also cause Postgres to treat the error in the question above as a full blown fatal error, not just a warning (because again, it doesn't actually know that it's okay for that particular command to fail). 使用--exit-on-error--single-transaction选项运行pg_restore可以解决这个问题,但也会导致Postgres将上述问题中的错误视为一个完整的致命错误,而不仅仅是一个警告(因为再一次,它实际上并不知道该特定命令失败是可以的。

The best solution to this is to take steps to prevent the error from happening in the first place. 解决此问题的最佳方法是采取措施防止错误发生。 In this case, you'd probably do this by dropping tables with a separate command before running pg_restore , and leaving off the --clean option. 在这种情况下,您可能通过在运行pg_restore之前使用单独的命令删除表 ,并且不使用--clean选项来执行--clean

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM