postgresql 如何设置 Spring/Heroku/postgres SSL 数据源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11832361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 00:09:13  来源:igfitidea点击:

How to setup Spring/Heroku/postgres SSL datasource

springpostgresqlsslheroku

提问by Joan Camps Morey

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

我正在尝试从我的 Heroku/Spring 应用程序到 postgres.heroku.com postgres 数据库创建一个数据源。这是我的applicationContext.xml片段。

<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:

但是当我运行该应用程序时,tomcat 出现此错误:

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

如何配置它以使用 SSL?我需要它来建立与 postgres.heroku.com 的连接

回答by DB5

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

您需要使用相关的 ssl 信息扩展您的 JDBC 连接 URL。

Your JDBC connection URL will need to include the following:

您的 JDBC 连接 URL 需要包含以下内容:

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

So in your case the URL would be:

所以在你的情况下,URL 将是:

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

此信息的来源:https: //devcenter.heroku.com/articles/connecting-to-relational-databases-on-heroku-with-java#connecting-to-a-database-remotely

回答by iaL

Following worked for me:

以下对我来说有效:

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

I'd to change ssl=trueto sslmode=require

我倒是要改变ssl=truesslmode=require

回答by Rhino

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

使用 jdbc 连接到 Heroku Postgres 时,有时也会出现错误,除非您将 SSL 参数添加到 url:

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

回答by nsthethunderbolt

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)

我犯的错误是使用 & 而不是&amp;(我知道很傻)

 ?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

是的,链接绝对有用: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 postgresqlinstead) if No suitable Sql driver error occurs

并记住:在此链接中,使用了 jdbc: postgres://host..,如果发生 No合适的 Sql 驱动程序错误,这可能不适合您(使用postgresql代替)