Search code examples
javaprocessbuilder

ProcessBuilder doesn't create file on server


I'm using process builder to run psql command from postgres to call copy commands to export some tables.

My workflow are the following:

String sql = "\"\\copy ( SELECT * from table ) TO '/folder' DELIMITER '|' NULL '\\N'\";

List<String> commands = new ArrayList<>();
commands.add("sudo");
commands.add("-u");
commands.add("root");
commands.add("psql");
commands.add("-h");
commands.add("host");
commands.add("-U");
commands.add("postgres");
commands.add("-d");
commands.add("database");
commands.add("-c");
commands.add(sql);

ProcessBuilder process = new ProcessBuilder(commands);

Process execution = process.start();
execution.waitFor(); // each calling to export data wait a little to generate next;

The following commands will turns on final in that commands:

sudo -u root psql -h host -U postgres -d database_user -c \copy ( select * from table TO "/folder/file.txt/" DELIMITER '|' NULL '\\N'" )

But the file.txt isn't created in server, even run as root.

Local works fine. But when is ran within server the commands don't runs. Where are the problem?


Solution

  • I've solved this problem.

    Just remove the quotation marks of "-c" argument, that is, without scaping the final string.

    Instead:

    sudo -u root psql -h host -U postgres -d database_user -c \copy "( select * from table TO '/folder/file.txt/') DELIMITER '|' NULL '\\N'
    

    I used:

    sudo -u root psql -h host -U postgres -d database_user -c \copy ( select * from table TO '/folder/file.txt/') DELIMITER '|' NULL '\\N'