Spring Boot JPA cannot find the PostgreSQL table it maps to
org.springframework.dao.InvalidDataAccessResourceUsageException: JDBC exception executing SQLShort answer
Spring Boot maps an entity to a lowercase snake_case table name and creates tables on its own only for embedded databases. On PostgreSQL the table is therefore either missing or stored under a different name. Compare the SQL in the log with the real table, then change the naming strategy or create the schema.
Fix it fast
1Find out which cause you have
Print the SQL Hibernate sends and read the table name in it, then list what PostgreSQL really has. The two names must match exactly.
# application.properties spring.jpa.show-sql=true # shell psql -d mydb -c '\dt'2Match what you see, then jump to its fix
What does the error look like?
org.springframework.dao.InvalidDataAccessResourceUsageException: JDBC exception executing SQLERROR: relation "telephone_number" does not exist
Cause 1 of 2
The table was created with a different case or style than Boot generates
1Check
By default Boot uses CamelCaseToUnderscoresNamingStrategy: dots and camel case become underscores and table names are lower case, so a TelephoneNumber entity maps to telephone_number.
psql -d mydb -c '\dt'2Fix
Either name the tables the way Boot generates them, or switch to Hibernate's own naming so the names in your mappings are used as written.
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl3Verify
Restart the app and repeat the request that failed.
curl -i http://localhost:8080/telephone-numbersConfirmed by docs.spring.io
Cause 2 of 2
Nothing created the table
1Check
JPA databases are created automatically only for an embedded database such as H2. For other databases, ddl-auto defaults to none unless a schema manager such as Flyway or Liquibase is in use.
grep -rn "ddl-auto\|flyway\|liquibase" src/main/resources/ pom.xml build.gradle*2Fix
Create the schema with migrations. Flyway and Liquibase are the schema managers Spring Boot supports; add one and put the CREATE TABLE statements in its first migration.
-- src/main/resources/db/migration/V1__create_telephone_number.sql
CREATE TABLE telephone_number (
id BIGSERIAL PRIMARY KEY,
number VARCHAR(30) NOT NULL
);3Verify
Restart the app, then list the tables again.
psql -d mydb -c '\dt'Confirmed by docs.spring.io
How do I stop it coming back?
- Treat schema as code: keep migrations in the repository and run them on every deploy.
- Use the same naming strategy in every environment.
- Run integration tests against PostgreSQL, not only H2, so naming differences show up early.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Databases
ERROR: relation does not exist for a table that is there
LINE 1: SELECT * FROM users;
The name you typed does not resolve to the table you see.
3 causes, each with a fixDatabases
H2 in PostgreSQL mode still behaves differently from PostgreSQL
Names or null ordering differ from PostgreSQL in test results
H2 compatibility modes are switched on in the JDBC URL and cover only a small part of the differences between databases.
4 causes, each with a fixDatabases
Hibernate insert fails: column is of type bit but expression is of type boolean
ERROR: column "active" is of type bit but expression is of type boolean
PostgreSQL has a real boolean type and a separate bit type, and it will not convert one to the other on insert.
3 causes, each with a fixDatabases
Spring Boot cannot configure a DataSource for PostgreSQL
Failed to determine a suitable driver class
Spring Boot builds a pooled DataSource only when it has a JDBC URL and a loadable driver.
3 causes, each with a fixDatabases
Spring Boot stops at startup with flywayInitializer failing
Caused by: org.flywaydb.core.api.FlywayException
flywayInitializer is the bean that runs your migrations at startup, so any migration problem surfaces as its BeanCreationException.
4 causes, each with a fixDatabases
Spring Boot cannot use the PostgreSQL JDBC URL it was given
java.lang.RuntimeException: Driver org.postgresql.Driver claims to not accept jdbcUrl
The message means the URL string handed to the driver is not a PostgreSQL JDBC URL.
3 causes, each with a fix