You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot项目连接PostgreSQL数据库报错求助

Fixing InvocationTargetException After Configuring PostgreSQL in Spring Boot

Let’s work through this problem step by step — that InvocationTargetException is just a wrapper for the real underlying error, so we’ll dig into the most common causes first.

First, Fix the Obvious Configuration Issue

Looking at your application.properties, you have a trailing space after the equals sign in your datasource URL:

spring.datasource.url= jdbc:postgresql://10.1.2.5/vlex

That space gets included in the URL string, which the PostgreSQL JDBC driver can’t parse correctly. Remove the space immediately:

spring.datasource.url=jdbc:postgresql://10.1.2.5/vlex

This is one of the most frequent culprits for connection-related errors after adding configs.

Next, Verify the Database Connection Details

Even with the fixed URL, double-check these:

  • PostgreSQL Service Status: Is the PostgreSQL server running on 10.1.2.5? Can you ping that IP from your machine?
  • Port Number: If your PostgreSQL instance isn’t using the default port (5432), add it to the URL: jdbc:postgresql://10.1.2.5:5433/vlex (replace 5433 with your actual port).
  • Credentials & Permissions: Does the myUser account have read/write access to the vlex database? Test this with a tool like pgAdmin or the psql command line — if you can’t connect there, Spring won’t either.

Check Your PostgreSQL Dependency

Make sure your build file (Maven pom.xml or Gradle build.gradle) has the correct driver dependency, and that it’s compatible with your PostgreSQL server version:

Maven:

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <scope>runtime</scope>
</dependency>

Gradle:

runtimeOnly 'org.postgresql:postgresql'

If your server is on a newer version (e.g., 14+), use a driver version 42.2.x or higher to avoid compatibility gaps.

Debug with Detailed Logs

To see the real error behind InvocationTargetException, look for the Caused by: section in your console output — that’s where the actual problem lives. You can also enable more verbose JPA logs to get clarity:
Add these to application.properties:

spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
logging.level.org.hibernate=INFO

This will show you exactly what Hibernate is doing when trying to connect to the database, which can point to issues like missing dialect configuration.

Optional: Explicitly Set the Hibernate Dialect

Adding this line to your config can prevent Hibernate from guessing the wrong dialect for PostgreSQL:

spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect

Test the Connection Outside Spring

Before troubleshooting further, confirm you can connect to the database using the same credentials with an external tool (pgAdmin, DBeaver, or psql). If that fails, the problem is with your database setup (firewall rules, service not running, user permissions) — not your Spring app.


内容的提问来源于stack exchange,提问作者Michu93

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:57:51