gpt4 book ai didi

postgresql - spring postgresql 无法连接到本地数据库

转载 作者:行者123 更新时间:2023-11-29 13:20:50 25 4
gpt4 key购买 nike

我正在尝试测试我的 Spring DAO 级别,但无法连接到我的本地数据库。这是配置。

是的,我使用端口 54321 而不是标准端口 5432:

@Bean
public DataSource dataSource(){
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setPassword("password");
dataSource.setUsername("postgres");
dataSource.setUrl("jdbc:postgresql://localhost:53421/mydbtest");
dataSource.setDriverClassName("org.postgresql.Driver");

return dataSource;
}

@Bean
public JdbcTemplate jdbcTemplate(){
JdbcTemplate template = new JdbcTemplate();
template.setDataSource(dataSource());

return template;
}

@Bean
public DataSourceInitializer dataSourceInitializer(DataSource dataSource)
{
DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
dataSourceInitializer.setDataSource(dataSource);
dataSourceInitializer.setEnabled(true);
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
databasePopulator.addScript(new ClassPathResource("src/resource/sql/01_init_database.sql"));
dataSourceInitializer.setDatabasePopulator(databasePopulator);
return dataSourceInitializer;
}

我关闭了防火墙,尝试了不同版本的 postgresql 驱动程序。但我仍然得到这个异常(exception)。他还写信给我检查我的用户密码端口数据,但我确信它们是正确的:

Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)

还有什么问题吗?

我使用的是 postgresql 9.2

更新

如果重要的话,我在 Windows 上

这是 pg_hba.conf 的行

host    all             all                 0.0.0.0/0           trust

(我也试过md5)

这里是 postgres.conf 的一个片段

listen_addresses = '*'      # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
port = 54321 # (change requires restart)
max_connections = 100 # (change requires restart)

最佳答案

尝试连接到本地主机上的 PostgreSQL 服务器:

psql -h localhost -p 53421

你能连接吗?如果不是,错误是什么?


编辑您的 PostgreSQL 配置:

pg_hba.conf

host all all 0.0.0.0/0 md5

postgresql.conf

    listen_addresses='*'
port = 53421

重启服务器。

(它没有连接到 Spring,而是 PostgreSQL 配置问题)

关于postgresql - spring postgresql 无法连接到本地数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42051449/

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