How would I use ExpectJ to call pg_dump (on PostgreSQL 8.4) in Java?

768 Views Asked by At

I've seen this problem many places, that is to say the problem with programmatically making backups from PostgreSQL. My solution is to use ExpectJ, but I'm having trouble getting my code to work.

I have the following code:

public class BackupUtility 
{
 public static void main(String[] args)
 {

  try
  {
   ExpectJ exp = new ExpectJ(20);
   Spawn shell = exp.spawn("\"C:\\Program Files\\PostgreSQL\\8.4\\bin\\pg_dump.exe\" " +
                           "-h localhost -U myUserName myDB");
   shell.expect("Password: ");
   shell.send("myPassword");

   System.out.println(shell.getCurrentStandardOutContents());

   shell.expectClose()
  }
  catch (Exception e) 
  {
   e.printStackTrace();
  }   
 }
}

However, on the shell.expect line, it's timing out.

When I run this command from the command prompt, it looks like the following:

C:\Documents and Settings\bobjonesthe3rd>"C:\Program Files\PostgreSQL\8.4\bin\pg_dump" -h localhost -U myUserName myDB
Password:

So, I know it prompts for a password, but ExpectJ isn't receiving the prompt for some reason.

Any help would be much appreciated.

Thanks, Matt

1

There are 1 best solutions below

4
On BEST ANSWER

I'm pretty sure an easier way to do that, without the need to use expect and send, is via a .pgpass file. Also referenced in the pg_dump docs.