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

标签 postgresql ssl jdbc datasource

在 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/

相关文章:

sql - Rails 中的原始数据库查询

sql - PostgreSQL - 如何获取按两列分组的数据?

java - SSLContext 和 PKCS12 keystore - 适用于 JVM 但不适用于 Android

postgresql - Postgres 查询比较两个日期是否等于边距

javascript - Servoy - 从数据库表中获取所有数据以显示为列表

ssl - tomcat 7 ssl 配置 : can multiple certificates be created for an ip address?

curl ca-cert 在 Mac 上安装 Meteor 时出错

java - 为什么使用 JPA/Eclipselink/Texo 进行批量插入比使用纯 JDBC 插入慢得多(!)?

java - 如果数据库关闭,将数据保存在磁盘上以供以后重试的正确方式是什么?

java - 如果 java.sql.Connection#commit() 抛出异常,是否需要回滚?