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

SqlPostgresqlEscaping

Sql Problem Overview


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.

Sql Solutions


Solution 1 - Sql

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";

Solution 2 - Sql

Mix double quotes and single quotes as such:

psql --command='create database "db-name-with-dashes"'

Solution 3 - Sql

Backticks ` is the quoted identifier used to reference the database:

ALTER DATABASE one RENAME TO `one-two`;

Solution 4 - Sql

psql -d -c "CREATE EXTENSION IF NOT EXISTS "'"uuid-ossp"'";"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShamal KarunarathneView Question on Stackoverflow
Solution 1 - SqlJoe StefanelliView Answer on Stackoverflow
Solution 2 - Sqlagent_smithView Answer on Stackoverflow
Solution 3 - SqlZafar FaheemView Answer on Stackoverflow
Solution 4 - SqlFrank TrezzaView Answer on Stackoverflow