【问题标题】:Running mvn clean install app tries to connect postgres to wrong port运行 mvn clean install app 尝试将 postgres 连接到错误的端口
【发布时间】:2020-01-24 13:10:04
【问题描述】:

运行时出现此错误

$ mvn clean install

org.postgresql.util.PSQLException: Connection to localhost:5432
refused. Check that the hostname and port are correct and that the
postmaster is accepting TCP/IP connections.

然而,在我的 application.properties 上,我已明确将端口定义为 5433:

spring.datasource.url=jdbc:postgresql://localhost:5433/mydb

我还在 postgres.conf 中所述的端口 5433 上运行 postgres:

port = 5433                             # (change requires restart)

我确实跑了

$ systemctl postgresql restart

更改端口后。

这里有什么问题? 我没有在项目文件的其他任何地方定义端口。

完整的 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@

【问题讨论】:

  • 很明显你的应用没有使用你认为应该使用的配置
  • 我确信在 spring-boot 项目中,除了 application.properties 文件之外,没有其他地方可以为 postgres 设置端口。我唯一能想到的是,该值存储在以前运行的某个位置。
  • 附带说明:为什么您的构建过程需要 Spring 来连接 DBMS?
  • @Amadán 我不明白你的问题。我有一个 springboot 项目,我使用 postgres db 来保存体育赛事分数。在构建过程中我无法连接到它,因为 maven build 使用了我定义的不同端口。
  • 你能添加你的 pom.xml 吗?

标签: java spring postgresql maven


【解决方案1】:

问题出在您的配置文件中(您没有提供)。如果您使用的是 spring boot,您很可能有一个 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 文件中指定的内容,或者您​​正在将其覆盖在某处。

*编辑

@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;
    }
}

【讨论】:

  • 我没有这样的文件。我在 application.properties 中定义了这些。从中我只包含了必要的文件“spring.datasource.url=jdbc:postgresql://localhost:5433/mydb”
  • 是否需要“数据源”文件?程序编译正确。
  • 是的,要覆盖自动配置,您需要一个使用 Configuration 注释的文件,在该文件中定义数据源 Bean。它可以编译,因为当你有一个 postgres 依赖项时,它会自动搜索默认的 postgres 端口 - 5432。
  • 我的 postgres 依赖如下: org.postgresqlpostgresqlruntime跨度>
  • 我已将您需要的课程添加为我的答案的编辑。您还必须在 application.properties 文件中定义其余属性,并且您应该能够访问数据库。
【解决方案2】:
  1. 打开cmd
  2. 写信netstat -ao | find "5433"
  3. 你会得到
TCP    0.0.0.0:8089           DESKTOP-Q687KK9:0      LISTENING       7732

我们使用端口7732

  1. 执行此命令Taskkill /pid 7732 /F

输出:SUCCESS: The process with PID 7732 has been terminated.

  1. 运行systemctl postgresql restart

【讨论】:

  • 为了澄清,我使用 Ubuntu 18.04。
  • 我也相信 systemctl restart postgresql 做同样的事情。
猜你喜欢
  • 2021-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多