Spring Boot项目连接PostgreSQL数据库报错求助
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
myUseraccount have read/write access to thevlexdatabase? Test this with a tool like pgAdmin or thepsqlcommand 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

