Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

PostgreSQL rejects a name or value that contains a hyphen

ErrorERROR: 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.

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

Fix it fast

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

    1. 1The 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. 2The 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. 3The 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

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

Did this fix it?

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

Did this fix it?

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=true

3Verify

Start the app with spring.jpa.show-sql=true and check the statement shows the quoted name.

spring.jpa.show-sql=true

Confirmed by docs.spring.io

Did this fix it?

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

Next step

Want this handled for good?

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