18

I'm trying to create a datasource from my Heroku/Spring application to postgres.heroku.com postgres database. Here is my applicationContext.xml snippet.

<bean id="securityDataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="org.postgresql.Driver"/>
    <property name="url" value="jdbc:postgresql://url/dstabase"/>
    <property name="username" value="user"/>
    <property name="password" value="password"/>
</bean>

But when I run the app, tomcat gives this error:

Cannot create PoolableConnectionFactory (FATAL: no pg_hba.conf entry for host "10.11.12.13", user "username", database "database", SSL off)

How can I configure it to use SSL? I need it in order to stablish a connection against postgres.heroku.com

3
  • 1
    Got to admit I've never used PostgreSQL or Heroku but am guessing the following should help: devcenter.heroku.com/articles/… Commented Aug 6, 2012 at 18:55
  • @DB5 that sounds correct, you should put it as an answer Commented Aug 6, 2012 at 21:16
  • how did you resolve this? Commented Mar 27, 2014 at 13:37

4 Answers 4

30

You need to extend your JDBC connection URL with the relevant ssl information.

Your JDBC connection URL will need to include the following:

ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory

So in your case the URL would be:

jdbc:postgresql://url/dstabase?ssl=true&amp;sslfactory=org.postgresql.ssl.NonValidatingFactory

Source for this info: https://devcenter.heroku.com/articles/connecting-to-relational-databases-on-heroku-with-java#connecting-to-a-database-remotely

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

2 Comments

this setting opens a SSL connection, but does not validate the certificates. If you have the chance to use certificates as described here: jdbc.postgresql.org/documentation/head/ssl-client.html
for me it worked using ...?ssl=true&&sslfactory=org.postgresql.ssl.DefaultJavaSSLFactory
5

Following worked for me:

jdbc:postgresql://url/databaseName?sslmode=require&amp;sslfactory=org.postgresql.ssl.NonValidatingFactory

I'd to change ssl=true to sslmode=require

Comments

4

When connecting to Heroku Postgres with jdbc, you will also sometimes get an error unless you add the SSL parameters to the url:

ssl=true
sslfactory=org.postgresql.ssl.NonValidatingFactory

Comments

3

It worked for me:

  <bean id="securityDataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="org.postgresql.Driver"/>
        <property name="url" value="jdbc:postgresql://host:port/database?ssl=true&amp;sslfactory=org.postgresql.ssl.NonValidatingFactory"/>
        <property name="username" value="user"/>
        <property name="password" value="password"/>
    </bean>

The mistake I was doing was using an & instead of &amp; (silly I know)

 ?ssl=true&amp;sslfactory=org.postgresql.ssl.NonValidatingFactory

and yes the link definitely is helpful: https://devcenter.heroku.com/articles/connecting-to-relational-databases-on-heroku-with-java#connecting-to-a-database-remotely

AND Keep in mind: in this link, jdbc:postgres://host.. is used, which might not be suitable for you (use postgresql instead) if No suitable Sql driver error occurs

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.