由于不支持身份验证类型 10,无法连接到 Postgres DB

新手上路,请多包涵

我最近尝试了 Postgres。将其安装在本地(PostgreSQL 13.0)。创建了一个 Maven 项目并使用了 Spring Data JPA,效果很好。而当我尝试使用 Gradle 项目时,我无法连接到数据库并不断收到以下错误。

org.postgresql.util.PSQLException:不支持身份验证类型 10。检查您是否已将 pg_hba.conf 文件配置为包含客户端的 IP 地址或子网,并且它正在使用驱动程序支持的身份验证方案。在 org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:614) ~[postgresql-42.1.4.jar:42.1.4] 在 org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java: 222) ~[postgresql-42.1.4.jar:42.1.4] 在 org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) ~[postgresql-42.1.4.jar:42.1.4] 在 org. postgresql.jdbc.PgConnection.(PgConnection.java:194) ~[postgresql-42.1.4.jar:42.1.4] 在 org.postgresql.Driver.makeConnection(Driver.java:450) ~[postgresql-42.1.4. jar:42.1.4] at org.postgresql.Driver.connect(Driver.java:252) ~[postgresql-42.1.4.jar:42.1.4] at java.sql.DriverManager.getConnection(Unknown Source) [na: 1.8.0_261] 在 java.sql.DriverManager.getConnection(未知来源)[na:1.8.0_261] 在 org.postgresql.ds.common.BaseDataSource.getConnection(BaseDataSource.java:94)[postgresql-42.1.4.jar :42.1.4] 在 org.postgresql.ds.common.BaseDataSource.getConnection(BaseDataSource.java:79) [postgresql-42.1.4.jar:42.1 .4]

我也尝试使用 JDBCTemplate。不起作用

参考 这篇 文章修改了 pg_hba.cfg 文件 - 不起作用

使用已弃用的 Lib of - 也不起作用。

请建议我解决这个问题。

我的代码和配置:

     @Configuration
    public class DataSourceConfig {


        @Bean
        public DriverManagerDataSource getDataSource() {
            DriverManagerDataSource dataSourceBuilder = new DriverManagerDataSource();
            dataSourceBuilder.setDriverClassName("org.postgresql.Driver");
            dataSourceBuilder.setUrl("jdbc:postgresql://localhost:5432/postgres");
            dataSourceBuilder.setUsername("postgres");
            dataSourceBuilder.setPassword("root");
            return dataSourceBuilder;
        }

    }

@Component
public class CustomerOrderJDBCTemplate implements CustomerOrderDao{

    private DataSource dataSource;

    private JdbcTemplate jdbcTemplateObject;

    @Autowired
    ApplicationContext context;

    public void setDataSource() {
        //Getting Bean by Class
        DriverManagerDataSource dataSource = context.getBean(DriverManagerDataSource.class);
        this.dataSource = dataSource;
        this.jdbcTemplateObject = new JdbcTemplate(this.dataSource);
    }

@Override
    public Customer create(Customer customer) {
        setDataSource();
        String sql = "insert into CustomerOrder (customerType, customerPayment) values (?, ?)";
        //jdbcTemplateObject.update(sql, customerOrder.getCustomerOrderType(), customerOrder.getCustomerOrderPayment());

        KeyHolder holder = new GeneratedKeyHolder();
        jdbcTemplateObject.update(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                ps.setString(1, customer.getType());
                ps.setString(2, customer.getPayment());
                return ps;
            }
        }, holder);

        long customerId = holder.getKey().longValue();
        customer.setCustomerID(customerOrderId);
        return customer;

    }

}

依赖关系

implementation('org.springframework.boot:spring-boot-starter-web')
    compile("org.springframework.boot:spring-boot-devtools")
    compile(group: 'org.postgresql', name: 'postgresql', version: '42.1.4')
    compile("org.springdoc:springdoc-openapi-ui:1.4.1")
    compile("org.springframework:spring-jdbc:5.2.5.RELEASE")

password_encryption 设置如下:

 postgres=# show password_encryption;
 password_encryption
---------------------
 scram-sha-256
(1 row)

原文由 Tushar Banne 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 2.8k
2 个回答

我通过在 PostgreSQL 版本 13 中应用以下步骤解决了类似问题:

  1. password_encryption 更改为 md5postgresql.conf
 Windows: C:\Program Files\PostgreSQL\13\data\postgresql.conf
GNU/Linux:           /etc/postgresql/13/main/postgresql.conf

在此处输入图像描述

  1. scram-sha-256 更改为 md5pg_hba.conf
 Windows: C:\Program Files\PostgreSQL\13\data\pg_hba.conf
GNU/Linux:           /etc/postgresql/13/main/pg_hba.conf

 host    all             all             0.0.0.0/0               md5

在此处输入图像描述

  1. 更改密码(此恢复密码为 md5 格式)。

示例: ALTER ROLE postgres WITH PASSWORD 'root'

  1. 如果您在非生产环境中工作,请确保在 postgresql.conf 中设置 listen_addresses = '*'

原文由 Kishor K 发布,翻译遵循 CC BY-SA 4.0 许可协议

在目录 C:\Program Files\PostgreSQL\13\data\pg_hba.conf 中获取您的 pg_hba.conf 文件

并简单地将 Column Method 下的 scram-sha-256 更改为 trust。

在此处输入图像描述

它对我有用!

原文由 Bukhari Syed 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题