我正在尝试测试我的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驱动程序。但我仍然得到这个例外。他还写信给我检查我的用户密码端口数据,但我确信它们是正确的:
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)
答案 0 :(得分:1)
尝试连接到localhost上的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配置问题)