Skip site navigation (1) Skip section navigation (2)

Re: Novice! How to run pg_dump from within Java?

From: "Michael Swierczek" <mike(dot)swierczek(at)gmail(dot)com>
To: "Damian C" <jamianb(at)gmail(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Novice! How to run pg_dump from within Java?
Date: 2006-07-26 13:27:41
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-novice
> On 7/25/06, Damian C <jamianb(at)gmail(dot)com> wrote:
> Hello,
> We have a Java project that is developing a small business app and
> using Hibernate & Postgres for persistence.  It all works like a dream
> until we need to know anything about Postgres!!  It is obviously a
> "feather in the cap" of Postgres that "so many (Java developers) can
> do so much while knowing so little".
> I need to perform the world's simplest task: Create a pg dump from within Java.
> Of course we do this easily using pgAdminIII, but I need to automate
> it for our Users. I notice that pgAdmin uses a command line like this
> ...
> <snip>
> C:\Program Files\PostgreSQL\8.1\bin\pg_dump.exe -i -h localhost -p
> 5432 -U pgsuperuser -F c -b -v -f "D:\PG_26jul06.backup" devdb
> </snip>
> We use "ProcessBuilder" to launch the  "pg_dump.exe" program. It takes
> an List of Strings in its constructor (including appropriate escape
> characters) ...
> <snip>
> List<String> cmds = new ArrayList<String>();
> cmds.add("C:\\Program Files\\PostgreSQL\\8.1\\bin\\pg_dump.exe");
> cmds.add("-i");
> cmds.add("-h");
> cmds.add("localhost");
> cmds.add("-p");
> cmds.add("5432");
> cmds.add("-U");
> cmds.add("pgsuperuser");
> cmds.add("-F");
> cmds.add("c");
> cmds.add("-b");
> cmds.add("-v");
> cmds.add("-f");
> cmds.add("\"D:\\PG_26jul06A.backup\"");
> cmds.add("devdb");
> Process process = new ProcessBuilder(cmds).start();
> </snip>
> Now this DOES work in that pg_dump.exe is invoked, and a new file is
> created at the correct location. However, the file size is zero
> (instead of the pgAdminIII version that is 47K)
> Any suggestions on what I'm doing wrong,
> or any pointers for a better way to do this??
> Many thanks,
> -Damian

       I'm not sure what the problem is.  But when you execute a
command line application from inside Java, you can capture the
resulting print messages which normally go to STDOUT and STDERR.
      I have a simple in-house application to do this. The code is
ugly, but it's all I need to get the job done.  I've pasted a few
snippets below.   You can use that to capture any messages that
pg_dump would otherwise print to the screen, to see what the problem
     I'm a relative Java newbie myself, someone with some experience
and a need for production-ready code can certainly make something


// the command to run
Process proc = Runtime.getRuntime().exec(myCmd);
// the STDOUT results
BufferedReader	bufIn = new BufferedReader(new
// the STDERR results
BufferedReader bufErr = new BufferedReader(new
// the STDIN stream I can use to send additional data.
BufferedWriter	bw = new BufferedWriter(new
int in;
    StringBuffer sb = new StringBuffer("");
    while (bufIn.ready())
	in =;
	if (in > -1 && in < 65535)
	String c = String.valueOf((char)in);
   while (bufErr.ready())
	in =;
	if (in > -1 && in < 65535)
	String c = String.valueOf((char)in);
catch (IOException ioe)
     System.err.println("IOException from reader, message: " +
     return null;
catch (InterruptedException ie)
     System.err.println("InterruptedException from reader, message: "
+ ie.getMessage());
    return null;

In response to


pgsql-novice by date

Next:From: Tom LaneDate: 2006-07-26 14:49:38
Subject: Re: case-insensitive Index
Previous:From: Sean DavisDate: 2006-07-26 13:24:31
Subject: Re: Lurking Wanna Be

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group