PostgreSQL rejects a name or value that contains a hyphen
ERROR: syntax error at or near "-"Short answer
PostgreSQL reads an unquoted hyphen as the minus operator, so a name like my-db or a value like 2025-01-01 breaks the statement. Wrap identifiers in double quotes and values in single quotes, or rename the object with an underscore. In Hibernate, quote the name in the mapping.
Fix it fast
1Find out which cause you have
Look at the text the error points to and find the hyphen in your statement. Is it inside a name (database, schema, table, column) or inside a value? That decides the cause.
psql -c 'CREATE DATABASE my-db;'2Match what you see, then jump to its fix
- 1
The pointer sits on a name such as my-db or user-profile in CREATE, ALTER or SELECTA database, table or column name contains a hyphenFix - 2
The pointer sits on the first hyphen inside a value such as 2025-01-01 or a UUIDA date, UUID or other text value is written without single quotesFix - 3
The failing statement in the log comes from the application, and the table or column name in @Table or @Column has a hyphenHibernate generates SQL with an unquoted hyphenated nameFix
- 1
What does the error look like?
ERROR: syntax error at or near "-"org.postgresql.util.PSQLException: ERROR: syntax error at or near "-"
Cause 1 of 3
A database, table or column name contains a hyphen
1Check
PostgreSQL identifiers must start with a letter or underscore and may then hold letters, underscores, digits and dollar signs. A hyphen is not in that list, so it is parsed as minus.
psql -c '\l'2Fix
Put the name in double quotes every time you use it, or create it with an underscore instead. Quoted names are case-sensitive.
CREATE DATABASE "my-db";
-- or, easier to live with:
CREATE DATABASE my_db;3Verify
List the databases and connect with the same quoted name.
psql -l
psql -d "my-db"Confirmed by postgresql.org
Cause 2 of 3
A date, UUID or other text value is written without single quotes
1Check
A string constant must be bounded by single quotes. Without them, a minus sign is an operator applied to the number before it.
psql -c "SELECT 2025-01-01;"2Fix
Quote the value with single quotes (not double quotes, which mean identifiers).
INSERT INTO events (id, day)
VALUES ('3f2504e0-4f89-11d3-9a0c-0305e82c3301', '2025-01-01');3Verify
Run the statement again and read the row back.
psql -c "SELECT * FROM events;"Confirmed by postgresql.org
Cause 3 of 3
Hibernate generates SQL with an unquoted hyphenated name
1Check
Spring Boot passes spring.jpa.properties.* to Hibernate unchanged, and its documentation shows hibernate.globally_quoted_identifiers for quoting every generated name.
grep -rn "@Table\|@Column" src/main/java | grep -- "-"2Fix
Rename it with an underscore, or turn on global quoting so Hibernate quotes every name it generates. Global quoting makes every name case-sensitive.
@Entity
@Table(name = "user_profile")
public class UserProfile { }
# or, for all names, in application.properties:
spring.jpa.properties.hibernate.globally_quoted_identifiers=true3Verify
Start the app with spring.jpa.show-sql=true and check the statement shows the quoted name.
spring.jpa.show-sql=trueConfirmed by docs.spring.io
How do I stop it coming back?
- Name databases, tables and columns in lowercase with underscores.
- Pass values to SQL as bind parameters, never by pasting them into the statement text.
- If you must keep a hyphenated name, quote it everywhere, including in scripts and backup commands.
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
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
Hibernate cannot find the sequence hibernate_sequence on PostgreSQL
ERROR: relation "hibernate_sequence" does not exist
Hibernate 6 no longer uses one shared hibernate_sequence.
3 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 fix