Search code examples
javapostgresqlpg-dump

Too many command-line arguments when calling pg_dump from java


After running into an issue on executing some queries as strings in Java for postgres, I went using string arrays, which solved my existing issues.

After the switch I am now having an issue with pg_dump, but not with pg_restore.

When I supply my method with the following array:

[time, ./pg_dump, -U, lehigh, -d, lehigh, -Fc, data/completedDb.dump]

I get the following error:

pg_dump: too many command-line arguments (first is "data/completedDb.dump")

ProcessBuilder produces the following for my execution:

time ./pg_dump -U lehigh -d lehigh -Fc data/completedDb.dump

And it works fine when I add the output arrow, and remove the data folder, on the command line.

time ./pg_dump -U lehigh -d lehigh -Fc > completedDb.dump

I'm running this through eclipse, in Java on a postgres database, using :

 Runtime.getRuntime().exec();

I've tried using Process.start() but got the same errors, so I'm totally dumbfounded at what I'm doing wrong.

Prior to this change, pg_dump was being executed properly as a single string. I don't want to go back to that method as I want to maintain consistency, but I also want to figure out what I'm doing wrong here.


Solution

  • You should use -f before the output file name, since by default pg_dump outputs to stdout.

    Try

    [time, ./pg_dump, -U, lehigh, -d, lehigh, -Fc, -f, data/completedDb.dump]