Java 运行mvn clean install应用程序试图将postgres连接到错误的端口
我在运行时遇到此错误Java 运行mvn clean install应用程序试图将postgres连接到错误的端口,java,spring,postgresql,maven,Java,Spring,Postgresql,Maven,我在运行时遇到此错误 $mvn干净安装 org.postgresql.util.PSQLException:与本地主机的连接:5432 拒绝。检查主机名和端口是否正确,以及 邮局主管正在接受TCP/IP连接。 但在application.properties中,我已将端口明确定义为5433: spring.datasource.url=jdbc:postgresql://localhost:5433/mydb 我还在postgres.conf中所述的5433端口上运行postgres: po
$mvn干净安装
org.postgresql.util.PSQLException:与本地主机的连接:5432
拒绝。检查主机名和端口是否正确,以及
邮局主管正在接受TCP/IP连接。
但在application.properties中,我已将端口明确定义为5433:
spring.datasource.url=jdbc:postgresql://localhost:5433/mydb
我还在postgres.conf中所述的5433端口上运行postgres:
port=5433#(更改需要重新启动)
我确实跑了
$systemctl postgresql重启
更换端口后
这里怎么了?
我没有在项目文件中的任何其他地方定义端口
完整的application.properties文件:
spring.main.banner-mode=off
logging.level.org.springframework=ERROR
spring.jpa.hibernate.ddl-auto=none
spring.datasource.initialization-mode=always
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5433/mydb
spring.datasource.username=myuser
spring.datasource.password=mypass
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.profiles.active=@spring.profiles.active@
问题出在配置文件中(您尚未提供)。如果您使用的是SpringBoot,则很可能有一个java文件,而不是xml文件,其中必须从application.properties文件调用您的属性
@Value("${spring.datasource.url}")
private String url;
对于数据源,您应该有一个@Bean定义,它应该如下所示:
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setDriverClassName(driverClassName);
return dataSource;
}
问题是,您要么没有将url设置为application.properties文件中指定的url,要么在下面的某个位置将其覆盖
*编辑
@Configuration
public class PostgreConfiguration {
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.driver_class_name}")
private String driverClassName;
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setDriverClassName(driverClassName);
return dataSource;
}
}
netstat-ao |查找“5433”
Taskkill/pid 7732/F
成功:PID为7732的进程已终止。
systemctl postgrestart
很明显,您的应用程序没有使用您认为应该使用的配置。我确信在spring启动项目中,除了application.properties文件之外,没有其他地方可以为postgres设置端口。我能想到的唯一一件事是,值存储在以前运行的某个地方。另一方面:为什么构建过程需要Spring连接到DBMS?@Amadán我不理解你的问题。我有一个springboot项目,我使用postgres db保存体育赛事分数。在构建过程中,我无法连接到它,因为maven构建使用了我定义的不同端口。您可以添加pom.xml吗?我没有这样的I文件。我在application.properties中定义了这些。从中我只包含了必要的文件“spring.datasource.url=jdbc:postgresql://localhost:5433/mydb是否需要“数据源”文件?程序可以正确编译。是的,要覆盖自动配置,您需要一个带有配置注释的文件,在该文件中定义数据源Bean。它之所以会编译,是因为当你有一个postgres依赖项时,它会自动在默认的postgres端口-5432上搜索。我的postgres依赖项如下:org.postgresql postgresql runtime我已经将你需要的类添加到我的答案中作为编辑。您还必须在application.properties文件中定义其余的属性,并且您应该能够访问数据库。为了澄清这一点,我使用了Ubuntu 18.04。另外,我相信systemctl restart postgresql也会做同样的事情。
TCP 0.0.0.0:8089 DESKTOP-Q687KK9:0 LISTENING 7732