简体   繁体   English

如何使用Java创建Postgres数据库的备份

[英]how to create backup of postgres database using java

i want to take backup of postgres database using java. 我想使用Java备份postgres数据库。 I am using following code for this 我为此使用以下代码
but this is not working and not generating dump. 但这不起作用并且不会生成转储。

String pgDump = "C:\\Program Files\\PostgreSQL\\9.2\\bin\\pg_dump";
          String dumpFile = "D:\\test\\"+ tenant.getTenantAsTemplate()+".sql";
          String sql = pgDump+" -h localhost -U postgres -P postgres " + tenant.getTenantAsTemplate()+" > "+dumpFile;
          Process p = Runtime.getRuntime().exec(sql);
          int time = p.waitFor();
          System.out.println("time is "+time);
          if(time == 0){
            System.out.println("backup is created");
          }
          else{
            System.out.println("fail to create backup");
          }

Here i am getting time is 1. 我在这里得到的时间是1。

This is also operating system dependent and we need also pg_dump . 这也取决于操作系统 ,我们还需要pg_dump is there any other way to generate backup of database without pg_dump? 没有pg_dump,还有其他方法可以生成数据库备份吗?

please reply soon. 请尽快回复。

No, there is no way to generate a database backup without pg_dump , using the regular SQL connection. 不,没有pg_dump ,就无法使用常规SQL连接生成数据库备份。 It's a bit of an FAQ, but the people who want the feature never step up to do the work to implement the feature in PostgreSQL. 这是一个常见问题解答,但是想要该功能的人从不加紧努力以在PostgreSQL中实现该功能。

I guess technically you could use a replication connection to do a physical base backup like pg_basebackup does, but that's not really what you want, requires copying all databases on the machine, and would be a lot of work. 从技术上讲,我想您可以像pg_basebackup一样使用复制连接来进行物理基础备份,但这并不是您真正想要的,它需要复制计算机上的所有数据库,并且将需要大量工作。

You should use the String[] form of Runtime.exec as I mentioned in a related answer regarding pg_restore . 您应该使用Runtime.execString[]形式,正如我在有关pg_restore的相关答案中提到的那样

You must also check the process exit value to see if it terminated successfully or not, and you must be careful to handle, not just swallow, any exceptions thrown. 您还必须检查进程出口值以查看其是否成功终止,并且必须小心处理(不只是吞下)引发的任何异常。

Your code fails to check the exit value, and I think it's probably generating a malformed command that's failing with a non-zero exit code, probably because you are not correctly quoting the path to pg_dump . 您的代码无法检查退出值,并且我认为它可能正在生成格式错误的命令,并以非零退出代码失败,这可能是因为您未正确引用pg_dump的路径。 To see what's wrong, print the final assembled command line, you'll see something like: 要查看出了什么问题,请打印最终的汇编命令行,您将看到类似以下内容的内容:

C:\Program Files\PostgreSQL\9.2\bin\pg_dump -h localhost ....

which cmd.exe will split into: 该cmd.exe将拆分为:

c:\Program
Files\postgresql\9.2\bin\pg_dump
-h
localhost

... etc ...等等

See the problem? 看到问题了吗?

Do not just quote the path to pg_dump to work around this. 不要只是引用路径pg_dump ,以解决这个问题。 Use the String[] form of exec and you won't have to, plus it'll work correctly for other things like accidental %environmentvars% in paths. 使用execString[]形式,您不必这样做,而且它可以正确处理其他事情,例如路径中意外的%environmentvars%

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

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