Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

Spring Boot JPA cannot find the PostgreSQL table it maps to

Errororg.springframework.dao.InvalidDataAccessResourceUsageException: JDBC exception executing SQL

Short 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.

  • Applies to Spring Boot 3.4, Hibernate ORM 6.6, PostgreSQL 17, Java 21
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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'
  2. 2Match what you see, then jump to its fix

    1. 1The log shows a lowercase snake_case name while \dt lists a CamelCase or quoted nameThe table was created with a different case or style than Boot generatesFix
    2. 2\dt does not list the table at all, in the database the app connects toNothing created the tableFix

What does the error look like?

  • org.springframework.dao.InvalidDataAccessResourceUsageException: JDBC exception executing SQL
  • ERROR: 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.PhysicalNamingStrategyStandardImpl

3Verify

Restart the app and repeat the request that failed.

curl -i http://localhost:8080/telephone-numbers

Confirmed by docs.spring.io

Did this fix it?

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

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.