0

I've a problem when i'm connecting to a postgresql database with jdbc. I've install postgresql 9.1 from a package download on the pgAdmin3 site (i needed gui). I'm connecting to db whith pgAdmin with no problems, but when i try to connect from java code, i've the sequent error:

org.postggresql.util.PSQLException: FATAL: password authentication failed for user postgres

the code that throws the exception is

public class ConnectionManager {
   private ConnectionManager(){};
   private static boolean driverLoad = false;
   private static final String pgDriver="org.postgresql.Driver";
   private static final String pgUrl="jdbc:postgresql:coffeeDB";
   private static final String usr="postgres";
   private static final String psw="password";

   public static Connection getConnection() throws ClassNotFoundException, SQLException {
       if(!driverLoad) {
           Class.forName(pgDriver);
           driverLoad=true;
       }
       return DriverManager.getConnection(pgUrl, usr, psw);
   }
}
4
  • 2
    Apparently you have supplied the wrong password. Btw: you should be using the url: jdbc:postgresql://servername/coffeeDB where servername is the name of the machine that runs PostgreSQL. Alternatively use localhost if it's on the same machine.
    – user330315
    Commented Apr 8, 2012 at 18:12
  • i've already try to do that, but it doesn't work. The password is correct, because is the same that i've used to connect the pgAdmin GUI to the db
    – giozh
    Commented Apr 8, 2012 at 18:16
  • I've solved. The url requires to specify also the port
    – giozh
    Commented Apr 8, 2012 at 18:31
  • Please post your solution as answer and mark it as the solution to your question, so that this question will be marked as answered and other people can easily find your solution. Thank you Commented Apr 8, 2012 at 19:46

2 Answers 2

1

I've solved. Just add the default port on the URL of postgresql

0

I was getting this in Play 2.2. A slightly different flavor

[error]c.j.b.h.AbstractConnectionHook - Failed to obtain initial connection Sleeping for 0ms and trying again. Attempts left: 0. Exception: null.Message:FATAL: password authentication failed for user "root"

Issue was quotes around the url in application.conf. If there are special characters, quotes are needed

Fails

db.default.url=jdbc:postgresql://myMachine:5432/mydb

Works

db.default.url="jdbc:postgresql://myMachine:5432/mydb"

Same goes for password

Fails

Passw)rd#!

Works

"Passw)rd#!"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.