Hibernate cannot find the sequence hibernate_sequence on PostgreSQL
Missing sequence or table: hibernate_sequenceShort answer
Hibernate 6 no longer uses one shared hibernate_sequence. It expects one sequence per entity, named after the entity, so an older schema fails validation or insert. Create the sequences, keep the old behaviour with a naming-strategy setting, or switch the entity to IDENTITY. Read the exact name in the error.
Fix it fast
1Find out which cause you have
List the sequences in the database and compare them with the name in the error.
psql -d mydb -c '\ds'2Match what you see, then jump to its fix
- 1
The upgraded app asks for a name like person_seq, or fails validation, while the database only has hibernate_sequenceHibernate 6 expects one sequence per entity, but the schema has the old shared oneFix - 2
The table has an auto-increment (serial or identity) id column and no sequence exists for itThe entity uses IDENTITY in the database but the mapping asks for a sequenceFix - 3
validate or inserts fail on a PostgreSQL database that the app did not createNo schema manager created the sequenceFix
- 1
What does the error look like?
Missing sequence or table: hibernate_sequenceERROR: relation "hibernate_sequence" does not exist
Cause 1 of 3
Hibernate 6 expects one sequence per entity, but the schema has the old shared one
1Check
The Hibernate 6.0 migration guide says implicit sequences are now one per entity hierarchy, named <entity name>_seq (Person uses person_seq), with an allocation size of 50. The shared hibernate_sequence is no longer the default.
grep -rn "GeneratedValue" src/main/java
psql -d mydb -c '\ds'2Fix
Either keep the old naming with the setting from the migration guide, or create the new sequences. The second option also needs the allocation size to match: 50 by default.
# keep the old behaviour
spring.jpa.properties.hibernate.id.db_structure_naming_strategy=legacy
-- or create the per-entity sequence (increment 50)
CREATE SEQUENCE person_seq START WITH 1 INCREMENT BY 50;3Verify
Insert a row through the app and read the new id.
psql -d mydb -c 'SELECT id FROM person ORDER BY id DESC LIMIT 3'Confirmed by raw.githubusercontent.com
Cause 2 of 3
The entity uses IDENTITY in the database but the mapping asks for a sequence
1Check
With GenerationType.AUTO on a numeric id, Hibernate uses a sequence-style generator. IDENTITY is the strategy that lets the database generate the id column and does not use a sequence object of Hibernate's.
grep -rn -B1 "private Long id" src/main/java2Fix
Switch the id mapping to IDENTITY. Hibernate then cannot batch inserts for that entity, which is the price of this strategy.
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;3Verify
Save a new entity; the id comes back from the database.
repository.save(new Person());Confirmed by docs.hibernate.org
Cause 3 of 3
No schema manager created the sequence
1Check
For a database that is not embedded, spring.jpa.hibernate.ddl-auto defaults to none unless Flyway or Liquibase manages the schema, so Spring Boot creates nothing.
grep -rn "ddl-auto\|flyway" src/main/resources/ pom.xml2Fix
Create the sequence in a migration, with the increment that matches the mapping.
-- db/migration/V2__person_sequence.sql
CREATE SEQUENCE IF NOT EXISTS person_seq START WITH 1 INCREMENT BY 50;3Verify
Restart the application and list the sequences.
psql -d mydb -c '\ds'Confirmed by docs.spring.io
How do I stop it coming back?
- Name the generator on each entity with @SequenceGenerator, so the sequence name does not depend on a Hibernate default that changes between versions.
- Read the Hibernate migration guide before you raise the major version, and test inserts against a copy of the production schema.
- Keep sequences in migrations, with the same increment as the mapping's allocation size.
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
Flyway cannot handle the PostgreSQL URL after upgrading to Flyway 10 or newer
No database found to handle jdbc:postgresql://localhost:5432/app
Since Flyway 10, each database has its own module.
4 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
PSQLException: column user0_.id does not exist in a Hibernate query
org.postgresql.util.PSQLException: ERROR: column user0_.id does not exist
Hibernate generated SQL that names a column PostgreSQL does not have under that exact name.
3 causes, each with a fixDatabases
Spring Boot fails on startup: HikariPool-1 exception during pool initialization
Caused by: ... (the underlying driver exception)
HikariCP opens one connection when the pool starts, and the startup fails when that connection fails.
3 causes, each with a fixDatabases
PostgreSQL cannot work out the type of a null query parameter from Hibernate
org.postgresql.util.PSQLException: ERROR: function lower(bytea) does not exist
PostgreSQL works out an unspecified parameter's type from the place where it is first used.
3 causes, each with a fixDatabases
JDBC cannot log in to PostgreSQL: authentication type 10
org.postgresql.util.PSQLException: The authentication type 10 is not supported.
Authentication type 10 is SCRAM-SHA-256, the password method PostgreSQL 14 and later use by default.
3 causes, each with a fix