Search code examples
javapostgresqlruntime.exec

Restore PostgreSQL database using java


im using the following code to 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 the rawDatabase.backup file but only one table is getting restored why the rest of the tables are not happening whats wrong in my code? thanks in advance!!!!


Solution

  • Finally I got the solution

    Runtime r = Runtime.getRuntime();
    Process p;
    ProcessBuilder pb;
    r = Runtime.getRuntime();
    pb = new ProcessBuilder( 
        "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");
    pb.redirectErrorStream(true);
    p = pb.start();
    InputStream is = p.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String ll;
    while ((ll = br.readLine()) != null) {
     System.out.println(ll);
    }