Search code examples
sqlpostgresqlidentifierquoting

Select a column where its name has quotation marks in postgresql


The word end is a reserved word in Postgres, so I have a column named "end" in my database. So how do i execute a query to select that column? Query:

"SELECT column1, column2, column3, "end" FROM table";

doesn't work. I have also tried some combinations with backslash but nothing helped. I know that there are many other ways to solve this issue (renaming the column :) ) but I would like to learn how to find a solution in my occasion.


Solution

  • The query string is

    SELECT column1, column2, column3, "end" FROM table
    

    No enclosing quotes around the query.

    Preparing this string in your client is a problem of the client language. In PHP, you could simply use single quotes, which would be the better choice here to begin with, since nothing needs to be interpreted inside the string:

    'SELECT column1, column2, column3, "end" FROM table'
    

    If you need double quotes to interpret parts of the string you could concatenate individual parts of the string or escape double quotes within ...

    Everything is simpler if you just don't use reserved words as identifiers to begin with.