271

I have a db table say, persons in Postgres handed down by another team that has a column name say, "first_Name". Now am trying to use PG commander to query this table on this column-name.

select * from persons where first_Name="xyz";

And it just returns

ERROR: column "first_Name" does not exist

Not sure if I am doing something silly or is there a workaround to this problem that I am missing?

5 Answers 5

497

Identifiers (including column names) that are not double-quoted are folded to lower case in PostgreSQL. Identifiers created with double quotes retain upper case letters (and/or syntax violations) and have to be double-quoted for the rest of their life:

"first_Name"                 -- upper-case "N" preserved
"1st_Name"                   -- leading digit preserved
"AND"                        -- reserved word preserved

But (without double-quotes):

first_Name   → first_name    -- upper-case "N" folded to lower-case "n"
1st_Name     → Syntax error! -- leading digit
AND          → Syntax error! -- reserved word

Values (string literals / constants) are enclosed in single quotes:

'xyz'

So, yes, PostgreSQL column names are case-sensitive (when double-quoted):

SELECT * FROM persons WHERE "first_Name" = 'xyz';

The manual on identifiers.

My standing advice is to use legal, lower-case names exclusively, so double-quoting is never required.

System catalogs like pg_class store names in case-sensitive fashion - as provided when double-quoted (without enclosing quotes, obviously), or lower-cased if not.

14
  • 5
    @ArtB: The SQL standard defines case insensitive identifiers, just like Postgres implements it. The only deviation: unquoted identifiers are folded to upper case in the standard, but pg lower-cases everything that isn't double-quoted. (Only relevant in rare corner cases.) Details in the manual here. Commented Oct 29, 2014 at 15:17
  • 1
    @adfs: I don't think I can explain it any better than I already did. For more, follow the link to the manual I provided repeatedly. Commented Nov 8, 2014 at 14:09
  • 29
    @adfs: In SQL, foobar, FOOBAR and FooBar are the same identifier. However "foobar", "FooBar" and "FOOBAR" are different identifiers
    – user330315
    Commented Apr 5, 2015 at 21:23
  • 8
    @a_horse_with_no_name yes, but under SQL foobar and FOOBAR are the same as "FOOBAR", under potgresql FOOBAR and foobar etc are the same as "foobar".
    – Jasen
    Commented Mar 14, 2016 at 1:55
  • 2
    @KamelMili: I suggest to ask your question as question, providing all necessary information. Comments are not the place. You can always link to this answer for context. And you can leave a comment with the link to your related question here (to also get my attention). Commented Mar 25, 2016 at 13:43
29

To quote the documentation:

Key words and unquoted identifiers are case insensitive. Therefore:

UPDATE MY_TABLE SET A = 5;

can equivalently be written as:

uPDaTE my_TabLE SeT a = 5;

You could also write it using quoted identifiers:

UPDATE "my_table" SET "a" = 5;

Quoting an identifier makes it case-sensitive, whereas unquoted names are always folded to lower case (unlike the SQL standard where unquoted names are folded to upper case). For example, the identifiers FOO, foo, and "foo" are considered the same by PostgreSQL, but "Foo" and "FOO" are different from these three and each other.

If you want to write portable applications you are advised to always quote a particular name or never quote it.

1
  • So basically the docs encourage spongebob case Commented Oct 2, 2023 at 18:15
16

The column names which are mixed case or uppercase have to be double quoted in PostgresQL. So best convention will be to follow all small case with underscore.

3
  • 3
    This is incorrect as per the explanation given by @erwin-brandstetter Commented Dec 9, 2015 at 19:35
  • 13
    How is this incorrect? If you have column names that are mixed case or upper case, in order to refer to them you need to put the identifier in double quotes. Commented Jan 25, 2018 at 18:14
  • No you don't. If you have names with upper-case letters, Postgres will lowercase them. And when you query with upper-case letters, Postgres will also lowercase that. So you only need to use double quotes if you used them in the CREATE statement. It's nit-picky, but hey we are programmers. You have to get it exactly right or it's just wrong. Commented Mar 15 at 13:38
5

if use JPA I recommend change to lowercase schema, table and column names, you can use next intructions for help you:

select
    psat.schemaname,
    psat.relname,
    pa.attname,
    psat.relid
from
    pg_catalog.pg_stat_all_tables psat,
    pg_catalog.pg_attribute pa
where
    psat.relid = pa.attrelid

change schema name:

ALTER SCHEMA "XXXXX" RENAME TO xxxxx;

change table names:

ALTER TABLE xxxxx."AAAAA" RENAME TO aaaaa;

change column names:

ALTER TABLE xxxxx.aaaaa RENAME COLUMN "CCCCC" TO ccccc;
1

You can try this example for table and column naming in capital letters. (postgresql)

//Sql;
      create table "Test"
        (
        "ID" integer,
        "NAME" varchar(255)
        )



//C#
  string sqlCommand = $@"create table ""TestTable"" (
                                ""ID"" integer GENERATED BY DEFAULT AS IDENTITY primary key, 
                                ""ExampleProperty"" boolean,
                                ""ColumnName"" varchar(255))";
1
  • Stupid pgsql SQL parser will not explain identifiers as case insensitive in sql query, but most RDMS DB will do. I found it will be better in pgAdmin SQL tool.
    – Tomex Ou
    Commented Jun 27, 2022 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.