title: How to connect to PostgreSQL server using encrypted password
body: <p>In usual scenarios, the connection string contains the password in plain text, but this can be captured by Wireshark, so I want to use the encrypted password in a connection string. I found the below excerpt from the Postgres documentation:</p>
  
  <blockquote>
    <p><strong>Encrypting Passwords Across A Network</strong> The MD5 authentication method
    double-encrypts the password on the client before sending it to the
    server. It first MD5-encrypts it based on the user name, and then
    encrypts it based on a random salt sent by the server when the
    database connection was made. It is this double-encrypted value that
    is sent over the network to the server. Double-encryption not only
    prevents the password from being discovered, but it also prevents another
    connection from using the same encrypted password to connect to the
    database server at a later time.</p>
  </blockquote>
  
  <p>If I understood it correctly, to get the salt, I need to connect to the database, but doean't that mean that the password can be spoofed?</p>
  
  <p>I tried to google around but did not found a satisfying solution. I want to understand how can I get the random salt from PostgreSQL server using java and then encrypt the plain password with it and use an encrypted password to establish a connection.</p>