我最近试过用Postgres。安装在本地(PostgreSQL 13.0)。创建了一个maven项目并使用了,工作正常。然而,当我尝试使用Gradle项目时,我无法连接到DB并继续获得以下错误。
org.postgresql.util.PSQLException:不支持身份验证类型10。检查是否已将pg_hba.conf文件配置为包含客户端的IP地址或子网,以及它是否使用驱动程序支持的身份验证方案。org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:614) ~PostgreSQL42.1.4.jar:42.1.4 at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:222) ~PostgreSQL42.1.4.jar:42.1.4 at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) ~PostgreSQL42.1.4.jar:42.1.4 at 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在org.postgresql.Driver.connect(Driver.java:252) ~postgresql-42.1.4.jar:42.1.4在java.sql.DriverManager.getConnection(未知源) na:1.8.0_261 at java.sql.DriverManager.getConnection(未知源) na:1.8.0_261在org.postgresql.ds.common.BaseDataSource.getConnection(BaseDataSource.java:94) PostgreSQL42.1.4.jar:42.1.4 at org.postgresql.ds.common.BaseDataSource.getConnection(BaseDataSource.java:79) PostgreSQL42.1.4.jar:42.1.4
我也尝试过使用JDBCTemplate。不管用
修改pg_hba.cfg文件引用这 post -不起作用
用了不推荐的-也不起作用。
请给我一个解决这个问题的建议。
我的代码和Config:
@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)
发布于 2020-10-13 13:37:15
我通过在PostgreSQL 版本13中应用以下步骤解决了类似的问题
password_encryption
中将md5
更改为postgresql.conf
Windows: C:\Program Files\PostgreSQL\13\data\postgresql.conf
GNU/Linux: /etc/postgresql/13/main/postgresql.conf
scram-sha-256
中将md5
更改为pg_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
ALTER ROLE postgres WITH PASSWORD 'root'
;postgresql.conf
中设置了postgresql.conf
。发布于 2021-06-09 07:23:14
在目录Files\PostgreSQL\13\data\pg_hba.conf C:\中获取pg_hba.conf文件
只需将列方法下的scram-sha-256更改为信任。
对我起作用了!
发布于 2021-01-17 08:40:43
根据维基,支持SCRAM-SHA-256
加密的JDBC驱动程序是42.2.0或更高版本。就我而言,司机是41.1.1岁。将其更改为42.2.0或以上。帮我修好了。
(Maven,pom.xml
):
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.0</version>
</dependency>
https://stackoverflow.com/questions/64210167
复制相似问题