Restore PostgreSQL database using java -
im using following code restore postgresql database using java
runtime r = runtime.getruntime(); process p; string cmd ="d:/program files/postgresql/9.1/bin/pg_restore.exe --host localhost --port 5432 --username postgres --dbname mytestqq --role postgres --no-password --verbose d:\sathish\rawdatabase.backup"; p = r.exec(cmd); i have 42 tables in rawdatabase.backup file 1 table getting restored why rest of tables not happening whats wrong in code? in advance!!!!
it's surprising command show works @ all, since you're failing quote spaces in command path. try:
string[] cmd = { "d:\\program files\\postgresql\\9.1\\bin\\pg_restore.exe", "--host", "localhost", "--port", "5432", "--username", "postgres", "--dbname", "mytestqq", "--role", "postgres", "--no-password", "--verbose", "d:\\sathish\\rawdatabase.backup" }; p = r.exec(cmd); changes:
- convert single-string form safer arguments array form of
execcall; - double backslashes in
rawdatabasepath since original command fails escape backslashes,\rcarriage return in string instead of\char followedrchar. - switch doubled backslashes instead of forward slashes on program path consistency. change doesn't matter.
also check return status of process. must use process.waitfor() once has exited use process.exitvalue() determine result. should examine stderr , stdout captured process object errors , logging information.
the reason program continues not work because:
- you have old
pg_restoreprocesses hanging around holding locks; and/or - you aren't consuming stdout , stderr
pg_restoreruns out of buffered pipe space , blocks writing on output stream.
this simpler if use processbuilder instead. processbuilder lets provide file streams write output , takes care of lot of you. must still wait process terminate , check return code though.
Comments
Post a Comment