3

I have a system running over Tomcat which executes a backup batch job every day, it calls pg_dump.exe from Java and stores its output to an SQL file.

When I'm executing Tomcat from the shell, it works perfectly, but when I start Tomcat as a Windows service the pg_dump.exe process stays there doing nothing, the SQL file is never written and the dump is never performed.

Is there any additional parameter or configuration required for Windows services? because it works without problems on a Unix-box.

Update: I'm using a Java class to call the operation.

Process p = new ProcessBuilder(
          "C:/postgres9/bin/pg_dump.exe",
          "-U", "myuser",
          "-h" , "localhost",
          "mydb").start();
storeToFile( p.getInputStream() );

Specs:

  • Tomcat 7.0.23
  • Postgresql 9.1
  • Windows 2008
  • 1
    How do you call pg_dump.exe? The parameters passed to it and possibly the caller environment would be interesting. From what you described so far, it can be anything (waiting for password is a usual suspect). – dezso Dec 10 '12 at 21:04
  • I thought that too, but why does it work without problems when I execute Tomcat from a shell? ...strange... – Timoteo Ponce Dec 10 '12 at 21:10
  • 1
    One thing I can think of is running from the application it does not see a variable that is present in the shell. – dezso Dec 10 '12 at 21:21
  • Aha! I see your point, the PGPASSWORD value might not be assigned. Let me check it. – Timoteo Ponce Dec 10 '12 at 21:24
  • You were right, the solution is here How to run pg_dump from java – Timoteo Ponce Dec 10 '12 at 21:44
4

dezso was right, the PGPASSWORD environment variable is present when executing Tomcat from a single shell, but not when it's started as a service. The solution is to put the value in the process environment:

 ProcessBuilder builder = new ProcessBuilder(
      "C:/postgres9/bin/pg_dump.exe",
      "-U", "myuser",
      "-h" , "localhost",
      "mydb");

 Map<String, String> env = builder.environment();
 env.put("PGPASSWORD", "supersecret");

 Process p = builder.start();
 storeToFile( p.getInputStream() );

Solution source is here: How to pg_dump from Java

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.