zenitsu
zenitsu

Reputation: 1

pg_dump command in java

I have to create a java program where the pg_dump will happen. I was able to execute the following command in cmd line, the backup file is getting created.

pg_dump -U postgres -h 122.1.1.1 -p 5050 databasename > D:\filename.sql

But when I'm trying to execute in java class, the backup file is not getting created. I am facing issue on how to execute the above command in java.

Backup of Postgresql database with pg_dump

I actually tried the answer present in the above. But the backup file is not being generated. Can someone please help me on this..

Also I tried the following

Process p;
ProcessBuilder pb;
pb = new ProcessBuilder("D:\\pgsql\\pgsql_new\\pgsql\\bin\\pg_dump.exe", "--host", "122.1.1.1","--port", "5050", "--username", 
        "postgres", "--no-password", "--format", "custom", "--blobs","--verbose", "--file", "D:\\filename.sql", "databasename"); 

try {
    final Map<String, String> env = pb.environment();
    env.put("PGPASSWORD", "password");
    p = pb.start();
    final BufferedReader r = new BufferedReader(new InputStreamReader(p.getErrorStream()));
    String line = r.readLine();
    while (line != null) {
        System.err.println(line);
        line = r.readLine();
    }

The above code is working fine, but it does not match my requirement.

According to the requirement, the path for the pg_dump must not be specified. Is there any other way where I can take the backup without mentioning any path.

Upvotes: 0

Views: 238

Answers (1)

Senthil
Senthil

Reputation: 2246

You can do that by setting the path variable . i.e., enviroment variables PATH you append this path : D:\pgsql\pgsql_new\pgsql\bin\. Then when you run pg_dump.exe it will start working from your java / from cmd prompts any path as well.

Ref : https://sqlbackupandftp.com/blog/setting-windows-path-for-postgres-tools/

Upvotes: 0

Related Questions