Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the Postgresql JDBC Driver support Pgpass authentication?

I am trying to connect to a Postgresql database from Java using the JDBC Driver and would like to use pgpass to authenticate.

My Postgresql server is correctly setup for password authentication and I have a local .pgpass file; I can connect using psql without having to provide the user's password. However, when I try to open a connection in Java, I get the following error:

org.postgresql.util.PSQLException: The server requested password-based authentication, but no password was provided.

I am using the following code:

String url = "jdbc:postgresql://localhost/test";
Properties props = new Properties();
props.setProperty("user","my_user");
Connection conn = DriverManager.getConnection(url, props);

My question is then: does the Postgres JDBC Driver support pgpass?

The only clue I could find is in SO and seems to indicate that since the driver does not use libpq, then it cannot use pgpass. I can't seem to find an authoritave answer anywhere though.

like image 639
Rodrigue Avatar asked Jan 12 '14 11:01

Rodrigue


People also ask

What is JDBC driver for PostgreSQL?

PostgreSQL JDBC Driver allows Java programs to connect to a PostgreSQL database using standard, database independent Java code. Is an open source JDBC driver written in Pure Java (Type 4), and communicates in the PostgreSQL native network protocol.

Is PostgreSQL JDBC compliant?

It provides a standard set of interfaces to SQL -compliant databases. PostgreSQL provides a type 4 JDBC driver. Type 4 indicates that the driver is written in Pure Java, and communicates in the database system's own network protocol.

What is the artifact name of the driver used by JDBC to connect to the PostgreSQL database?

The default driver of JDBC interpreter is set as PostgreSQL . It means Zeppelin includes PostgreSQL driver jar in itself. So you don't need to add any dependencies(e.g. the artifact name or path for PostgreSQL driver jar) for PostgreSQL connection.


Video Answer


2 Answers

No, PgJDBC doesn't read .pgpass.

$ cd projects/pgjdbc
$ git grep pgpass
$ 

Your Java program would need to open and parse .pgpass then do appropriate matching for the connection.

If you write a class to read and parse .pgpass and to match it for a given set of connection parameters, please submit it for inclusion in the PgJDBC driver, where it'd go under the package org.postgresql.util.

You might find it easier to instead modify the JDBC driver directly, looking up the password if none is specified in the connection parameters once the JDBC driver has already parsed the JDBC URL into host, port, etc.

like image 170
Craig Ringer Avatar answered Oct 11 '22 04:10

Craig Ringer


Here is a tool to read password from .pgpass file in Scala,

object DbUtil {
  def dbPassword(hostname:String, port:String, database:String, username:String ):String = {
    // Usage: val thatPassWord = dbPassword(hostname,port,database,username)
    // .pgpass file format, hostname:port:database:username:password
    val passwdFile = new java.io.File(scala.sys.env("HOME"), ".pgpass")
    var passwd = ""
    val fileSrc = scala.io.Source.fromFile(passwdFile)
    fileSrc.getLines.foreach{line =>
      val connCfg = line.split(":")
      if (hostname == connCfg(0)
        && port == connCfg(1)
        && database == connCfg(2)
        && username == connCfg(3)
      ) { 
        passwd = connCfg(4)
      }
    }
    fileSrc.close
    passwd
  }

  def passwordFromConn(connStr:String) = {
    // Usage: passwordFromConn("hostname:port:database:username")
    val connCfg = connStr.split(":")
    dbPassword(connCfg(0),connCfg(1),connCfg(2),connCfg(3))
  }
}
like image 39
Charlie 木匠 Avatar answered Oct 11 '22 05:10

Charlie 木匠