问题描述
我最近尝试过Postgres.将其安装在本地(PostgreSQL 13.0)上.创建一个maven项目并使用Spring Data JPA,效果很好.而当我尝试使用Gradle项目时,我无法连接到数据库并不断出现以下错误.
I have recently tried my hands on Postgres. Installed it on local(PostgreSQL 13.0).Created a maven project and used Spring Data JPA, works just fine. Whereas when I tried using Gradle project, I am not able to connect to the DB and keep getting the following error.
我也尝试使用JDBCTemplate.不起作用
I tried using JDBCTemplate as well. Doesn't work
参考此修改了pg_hba.cfg文件帖子-不起作用
Modified the pg_hba.cfg file referring to this post - Doesn't work
使用了已弃用的库-也不起作用.
Used the deprecated Lib of - Doesn't Work either.
请为我建议一个解决此问题的方法.
Please Suggest me a solution for this problem.
我的代码和配置:
@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)
推荐答案
我通过在PostgreSQL 版本13 中应用以下步骤解决了类似的问题:
I solved similar issue by applying below steps in PostgreSQL Version 13 :
-
将password_encryption更改为md5.
Change password_encryption to md5.
文件: C:\ Program Files \ PostgreSQL \ 13 \ data \ postgresql.conf
在主机设置中将scram-sha-256
更改为md5
.
Change scram-sha-256
to md5
in host settings.
文件: C:\ Program Files \ PostgreSQL \ 13 \ data \ pg_hba.conf .
全部托管0.0.0.0/0 md5
host all all 0.0.0.0/0 md5
更改密码(此恢复密码为md5格式).
Change Password ( this restore password in md5 format).
示例:ALTER ROLE postgres WITH PASSWORD 'root'
;
如果您非生产性工作,请确保设置listen_addresses = '*'
环境.
Make sure you set listen_addresses = '*'
if you are working non productionenvironment.
文件:C:\Program Files\PostgreSQL\13\data\postgresql.conf
这篇关于由于不支持身份验证类型10,因此无法连接到Postgress DB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!