views:

47

answers:

2

Hi, 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.

+3  A: 

Don't do that. If psql does not like hyphens in the database name, then don't do that.

eumiro
It's not that psql does not like hyphens. I can create new databases using pgadmin with hyphen in the name. eg. I can create a new database with the name 'one-two'. (Unfortunately I can't use pgadmin to rename database.)
Shamal Karunarathne
And in this case, I have no other option. :-( . Have to use hyphen.
Shamal Karunarathne
Most databases are not going to like the hyphen in object names. We always convert to underscore, which they happily accept. That way you don't always have to quote your object names, like you'll have to do now.
Scott Bailey
Yeah, I understand Scott. But I had to do this not because I'm handling the database. I just had to adapt to the application set-up conventions.
Shamal Karunarathne
+1  A: 

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";
Joe Stefanelli
This worked. Thanks Joe.
Shamal Karunarathne