Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's the escape sequence for hyphen (-) in PostgreSQL

I'm trying to rename a database to a name with a hyphen (-).

ALTER DATABASE one RENAME TO one-two; 

And psql returns an error:

ERROR:  syntax error at or near "-" 

What should I use as an escape sequence for "-" character or what's the way to do the above?

Note: I've tried the '\-' and didn't work as well.

Thanks.

like image 784
Shamal Karunarathne Avatar asked Oct 15 '10 13:10

Shamal Karunarathne


People also ask

How do you handle a hyphen in SQL?

In MySQL, use the back-ticks. In standard SQL, use double-quotes. No, the hyphen is an operator, and you can't use that in the middle of an identifier.

What are special characters in PostgreSQL?

Special character symbols are characters with a pre-defined syntactic meaning in PostgreSQL. They are typically disallowed from being used in identifier names for this reason, though as mentioned in the section on quoted identifiers, this restriction can usually be worked around with quotes if need be.

What character symbol do you use at the end of a PostgreSQL query?

Since these expressions are used on character or string fields, we must put the expression inside single quotes. The beginning of the expression is marked with a “^” operator and the end is marked with a “$” dollar sign. These two characters help us understand the start and the end of the expression.


2 Answers

Double quotes should do it. But you'll have to always use the quoted-identifier everywhere you reference the database.

ALTER DATABASE one RENAME TO "one-two"; 
like image 191
Joe Stefanelli Avatar answered Sep 28 '22 10:09

Joe Stefanelli


Mix double quotes and single quotes as such:

psql --command='create database "db-name-with-dashes"' 
like image 26
agent_smith Avatar answered Sep 28 '22 10:09

agent_smith