Error explanation
The file pg_hba.conf
(host-based authentication configuration file) is used to control the client authentication. This file is located in the database cluster's data directory.
The error java.sql.SQLException: FATAL: no pg_hba.conf entry for host "<your-host-ip>", user "<postgres-user>", database "<db-name>", SSL off
means that Postgres accepts SSL connections but your are trying to connect without SSL.
Secured connection
If a certificate is requested from the client during SSL connection startup, it means that the DBA has set the clientcert
parameter in pg_hba.conf
. Note that if clientcert
is not specified (or set to 0), the server will NOT insist that a client certificate be presented.
Check if database server is using SSL
Before trying to access your SSL enabled server from Java, make sure that you can get to it via psql
.
If you want to connect with a given user at a given port:
$ psql -h <server-ip> -p <server-port> -d <db-name> -U <user>
Or if you want to connect at the default port with your current user:
$ psql -h <server-ip>
You should see an such output if you have established a SSL connection:
$ psql -h <server-ip> -d <db-name> -U <user>
psql (11.2, Server 9.6.5)
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
Note that the last line contains 'SSL connection'.
JDBC connection parameters
ssl
must be set if the server required a secured connection. Note that both ssl=true
or ssl
work but ssl=true
is recommended for the coming future release of Postgres.
sslfactory
provides the class name to use as the SSLSocketFactory when establishing a SSL connection.
The JDBC driver provdies an option to establish a SSL connection without doing any validation (but it's risky!).
A non-validating connection is established via a custom SSLSocketFactory
class that is provided with the driver. Setting the connection URL parameter sslfactory=org.postgresql.ssl.NonValidatingFactory
will turn off all SSL validation.
This was my case and why I also got the same error you had. So, to establish the connection I have:
jdbc:postgresql:
References
?ssl=true
to the JDBC URI. – Craig Ringer