2

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 ?

1 Answer 1

2

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?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.