79

Simple question, is there any way to omit the double quote in PostgreSQL?

Here is an example, giving select * from A;, I will retrieve ERROR: relation "a" does not exist, and I would have to give select * from "A"; to get the real result.

Is there any way not to do the second and instead do the first on PostgreSQL?

5 Answers 5

109

Your problem with this query started when you created your table. When you create your table, don't use quotes.

Use this:

CREATE TABLE a ( ... );

Not this:

CREATE TABLE "A" ( ... );

The latter will make it so that you always have to quote it later. The former makes it a normal name and you can use SELECT * FROM a; or SELECT * FROM A;

If you can't just recreate your table, use the ALTER TABLE syntax:

ALTER TABLE "A" RENAME TO a;
Sign up to request clarification or add additional context in comments.

7 Comments

I use the table creator wizard, and everytime I write with capital-letter, it will automatically add the doublequote
I've never used the table creator wizard, but I would imagine there is an option in the software to not do that. It's not a very common practice to quote table names in postgres. It just makes your life harder.
when I said "wizard", it is what is given in pgAdmin III
I see. I just checked, and it appears you can't turn this feature off in pgAdmin. I would suggest typing the table names in lower case when creating so that pgAdmin doesn't think the table names need to be case sensitive.
Well, "A" is not the same as a, sure. But is "a" the same as a? Instead of “don't use quotes”, I feel the better advice is “don’t use uppercase”.
|
55

double quotes are required if you include capital letters in your table name in postgres

to avoid the requirements name your table "a"

4 Comments

nice answer... Is there any reason why PostgreSQL do that (force us to put doublequotes for a capital-letter table)?
Yes, the reason is "the ANSI standard says so".
Hi, thank you for your comment. Is there a way to enable/disable the ANSI behavior like MySQL does with SET SQL_MODE=ANSI_QUOTES; ? (ref: stackoverflow.com/questions/13884854/…)
Anyone have a link to that ANSI standard?
41

Postgresql has some particular behaviour in regard to quoting and case sentivity: it folds every non-quoted identifier to lower case (also at creation time) and then works case-sensitively.

Double quotes in identifiers are only needed when the identifier (table name, column name, etc) was defined (at schema creation time) with uppercase letters (some or all) and between double quotes.

In that case (which I advice against), when you use that identifier, you must type it in the same way: case sensitively (type upper/lower case letter exactly as defined) and between double quotes.

In other cases, you can use non-quoted identifiers and work always case-insensitively.

4 Comments

"...and work in case-unsensitive mode". How to do this exactly?
@zfm: as Steve says. You create your identifiers with lower case (or without quotes - it's the same thing), and then you can use them in case-insensitive mode (not quoting them).
sorry I misinterpret your sentence. I thought that there is an option to change that mode :D
@zfm: the sentence was indeed confusing, I changed it.
12

Don't use upper case letter in your table name or it's column name, if you are using such thing then the postgres will required double quote for accessing it.

Comments

5

Please see the detailed description of what is happening here.

The PostgreSQL server table names are case-sensitive, but forced to be lower-case by default: when you type CREATE TABLE AAA, it will become CREATE TABLE aaa before the query execution.

Double-quoted names keep their case as it was, so after CREATE TABLE "AaA" you get the table AaA and have to write it double-quoted again and again.

Have no idea why did they do so :)

3 Comments

I do have an idea why the SQL designers made it case-insensitive: to make it easier for the developers. The real question is why tools and people today seem to insist on double quotes, perhaps to write more impressive code?
The thing is, it's NOT case-insensitive. That would have been the easiest solution. Postgres is doing the worst possible thing.
This is ridiculous. It is case insensitive when table in lowercase. But if table have uppercase, its case sensitive and requires quotas. Seems they hate "CamelCase" names)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.