如何修复PostgreSQL中relation不存在的PSQLException异常
org.postgresql.util.PSQLException: ERROR: relation "xxxx" does not exist Hey there, let’s work through this PostgreSQL relation not found error—this is a super common gotcha when working with JPA/Hibernate and PostgreSQL, especially when you’re sure the tables exist. Here are the most likely fixes based on your scenario:
1. Double-Check Table Name Case & Spelling
PostgreSQL has a quirky behavior: it automatically converts unquoted table names to lowercase. So if your entity class uses @Table(name = "XXXX") but the actual table in the DB is xxxx (or vice versa if you created the table with quoted uppercase names), the DB won’t find it.
- Fix: Match the
namevalue in your@Tableannotation exactly to the database table name, including capitalization. If you created the table with quotes (like"XXXX"), you’ll need to keep the quotes in the annotation:@Table(name = "\"XXXX\"").
2. Add Missing Schema Configuration
If your tables aren’t in PostgreSQL’s default public schema, your entity classes won’t know where to look unless you specify the schema explicitly.
- Fix:
- Add the
schemaattribute to your@Tableannotation:@Table(name = "xxxx", schema = "your_custom_schema") - Or set a default schema globally (for Spring Boot apps, add this to
application.yml):spring: jpa: properties: hibernate: default_schema: your_custom_schema
- Add the
3. Verify Your Data Source is Pointing to the Right DB
It sounds obvious, but sometimes apps connect to a different database than the one you imported your scripts into (e.g., a test DB instead of dev).
- Fix: Check your
application.propertiesorapplication.ymlfor the datasource URL:
Use thespring.datasource.url=jdbc:postgresql://localhost:5432/your_correct_db_namepsqlcommand-line tool to connect to this URL and run\dtto confirm thexxxxtable exists there.
4. Fix Entity Class Inheritance Mapping Issues
Since you mentioned an abstract class, misconfigured inheritance annotations can throw off table mappings.
- Fix:
- If your abstract class is just a base for shared fields, make sure it’s annotated with
@MappedSuperclass(not@Entity—that would create a separate table for the abstract class). - Ensure your
Userentity’s@Tableannotation isn’t being overridden or inherited incorrectly from the abstract class. Double-check that all field mappings (@Column) match the database column names too.
- If your abstract class is just a base for shared fields, make sure it’s annotated with
5. Check Database User Permissions
Your app’s database user might not have access to the xxxx table. PostgreSQL restricts access by default, so even if the table exists, the user might lack SELECT/INSERT privileges.
- Fix: Log into PostgreSQL as a superuser and run:
GRANT ALL PRIVILEGES ON TABLE xxxx TO your_app_database_user; -- Or grant only the needed privileges: -- GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE xxxx TO your_app_database_user;
6. Prevent DDL from Overwriting Your Imported Tables
If your JPA configuration is set to create tables on startup (e.g., hibernate.hbm2ddl.auto=create), it might be overwriting the tables you manually imported.
- Fix: Change the DDL auto setting to
updateornonein your config:
This ensures your existing tables are preserved and only updated if entity mappings change.spring: jpa: hibernate: ddl-auto: update
Quick Troubleshooting Steps to Confirm the Root Cause
- Run
\dtin the PostgreSQL shell connected to your app’s database to list all tables—confirmxxxxis present and matches the case in your entity. - Look closely at the full stack trace in your console: the generated SQL query will show exactly which table/schema the app is trying to access, which can reveal mismatches.
内容的提问来源于stack exchange,提问作者Denis Stephanov

