postgresql - how to create backup of postgres database using java -


i want take backup of postgres database using java. using following code this
not working , 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 "+time);           if(time == 0){             system.out.println("backup created");           }           else{             system.out.println("fail create backup");           } 

here getting time 1.

this operating system dependent , need pg_dump. there other way generate backup of database without pg_dump?

please reply soon.

no, there no way generate database backup without pg_dump, using regular sql connection. it's bit of faq, people want feature never step work implement feature in postgresql.

i guess technically use replication connection physical base backup pg_basebackup does, that's not want, requires copying databases on machine, , lot of work.

you should use string[] form of runtime.exec i mentioned in related answer regarding pg_restore.

you must check process exit value see if terminated or not, , must careful handle, not swallow, exceptions thrown.

your code fails check exit value, , think it's generating malformed command that's failing non-zero exit code, because not correctly quoting path pg_dump. see what's wrong, print final assembled command line, you'll see like:

c:\program files\postgresql\9.2\bin\pg_dump -h localhost .... 

which cmd.exe split into:

c:\program files\postgresql\9.2\bin\pg_dump -h localhost 

... etc

see problem?

do not quote path pg_dump work around this. use string[] form of exec , won't have to, plus it'll work correctly other things accidental %environmentvars% in paths.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -