gpt4 book ai didi

postgresql - 尝试通过 JDBC 与 Postgres 进行 SSL 连接时出现 PSQLException "Could not open SSL root certificate file"

转载 作者:行者123 更新时间:2023-12-05 01:33:00 36 4
gpt4 key购买 nike

在 Java 15 中,将来自 jdbc.postgresql.org 的 PostgreSQL JDBC 驱动程序(版本 42.2.18,JDBC4.2)与此 DataSource 一起使用:

PGSimpleDataSource ds = new PGSimpleDataSource();
ds.setServerNames( new String[] { "my-server-address" } );
ds.setPortNumbers( new int[] { 25060 } );
ds.setSsl( true );
ds.setDatabaseName( "mydb" );
ds.setUser( "scott" );
ds.setPassword( "tiger" );
this.dataSource = ds;

...代码 Connection conn = this.dataSource.getConnection(); 抛出这个异常:

org.postgresql.util.PSQLException: Could not open SSL root certificate file /Users/basilbourque/.postgresql/root.crt.

我知道我的 Postgres 12 服务器设置为 SSL 连接,因为我的 IntelliJ Ultimate具有通过 SSL (TLS) 保护成功连接的数据库访问功能 ( DataGrip)。

那么我在 JDBC 中的 DataSource 配置出了什么问题?

最佳答案

感谢this Github issue pgjdbc 驱动程序的页面,我发现了 davecramer 的这篇文章:

As of 42.2.5 ssl=true implies verify-full as per the release notes. If you wish to get the old behaviour use sslmode=require

果然,将 ds.setSsl( true ); 替换为 ds.setSslMode( "require"); 允许我的 JDBC 驱动程序通过 DataSource 建立连接

        PGSimpleDataSource ds = new PGSimpleDataSource();
ds.setServerNames( new String[] { "my-server-address" } );
ds.setPortNumbers( new int[] { 25060 } );
ds.setSslMode( "require" ); // Replaces: ds.setSsl( true );
ds.setDatabaseName( "mydb" );
ds.setUser( "scott" );
ds.setPassword( "tiger" );
this.dataSource = ds;

我不知道这些 SSL/TLS 相关选项中的任何一个实际上在做什么,但这对我连接到我的 DigitalOcean 管理的 Postgres 数据库服务器很有帮助。

下面的代码片段现在运行成功:

        try
(
Connection conn = this.dataSource.getConnection() ;
Statement stmt = conn.createStatement() ;
)
{
String sql =
"""
SELECT uuid_generate_v1()
;
""";
try (
ResultSet rs = stmt.executeQuery( sql ) ;
)
{
while ( rs.next() )
{
UUID uuid = rs.getObject( 1 , UUID.class );
System.out.println( "uuid = " + uuid );
}
}
}
catch ( SQLException e )
{
e.printStackTrace();
}

关于postgresql - 尝试通过 JDBC 与 Postgres 进行 SSL 连接时出现 PSQLException "Could not open SSL root certificate file",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64939063/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com