abdou amer
abdou amer

Reputation: 849

Postgresql Backup and restore using java with password

I want to backup and restore a postgresql database using pg_dump. The issue is : In command line when i execute this command :

pg_dump -h localhost -p 5432 -U postgres  -F t -f test2.tar  myDatabase

i get this prompet mesage to provide the database password:

 Mot de passe :

So, it works

But, in the context of java: I can only do:

 String cmd = "pg_dump -U postgres -h localhost -p 5342";
                cmd += " -F t -f " + file.getCanonicalPath();
                cmd += " myDatabase";
                Process p = Runtime.getRuntime().exec(cmd);
                int result= p.waitFor();

if (result == 0) {
                    System.out.println("Backup created successfully");
                } else {
                    System.out.println("There is an error");
                }

The above code is not working. So, what i can do to provide the database password at runtime ?

Upvotes: 2

Views: 2218

Answers (1)

pleft
pleft

Reputation: 7915

From PostgreSQL documentation about pg_dump:

-w

--no-password

Never issue a password prompt. If the server requires password authentication and a password is not available by other means such as a .pgpass file, the connection attempt will fail. This option can be useful in batch jobs and scripts where no user is present to enter a password.

So can you try also passing the -w option on your pg_dumpcommand and provide a .pgpass file with the password?

Upvotes: 2

Related Questions