248

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?

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
5122014009
  • 3,766
  • 6
  • 24
  • 34

5 Answers5

464

Identifiers (including column names) that are not double-quoted are folded to lowercase in PostgreSQL. Column names that were created with double-quotes and thereby retained uppercase letters (and/or other syntax violations) have to be double-quoted for the rest of their life:

"first_Name"

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

Read the manual on identifiers here.

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

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
  • @Erwin Are upper case table names illegal according to an SQL standard or is this something PostGresSQL specific? – Sled Oct 29 '14 at 13:51
  • 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.](http://www.postgresql.org/docs/current/interactive/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS) – Erwin Brandstetter Oct 29 '14 at 15:17
  • @ErwinBrandstetter: Can you please explain what you meant by 'The SQL standard defines case insensitive identifiers, just like Postgres implements it'? When we say case insensitive, does it not mean that we can name identifiers with any upper and lower case combination and still be able to retrieve them with any combination of upper and lower cases as long as we get the identifier name right? In this can we say Postgres implements the identifier case insensitivity standard defined for SQL? – Asegid Debebe Nov 08 '14 at 08:38
  • 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. – Erwin Brandstetter Nov 08 '14 at 14:09
  • 26
    @adfs: In SQL, `foobar`, `FOOBAR` and `FooBar` are the same identifier. However `"foobar"`, `"FooBar"` and `"FOOBAR"` are different identifiers –  Apr 05 '15 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 Mar 14 '16 at 01:55
  • @ErwinBrandstetter hey sorry to bother you am using Uppercase Table name and am using jbdcimpl whatever that not too important i have sql like this `"select pseudo,Password,enabled from `UTILISATEUR` where Pseudo = ? "` my problem i can't double-quotes and even with those it doesn't work you seem like sql expert guy please help – Kamel Mili Mar 25 '16 at 13:31
  • 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). – Erwin Brandstetter Mar 25 '16 at 13:43
  • 1
    here and thank you http://stackoverflow.com/questions/36218161/reason-preparedstatementcallback-bad-sql-grammar?noredirect=1#comment60069706_36218161 – Kamel Mili Mar 25 '16 at 13:48
  • Good advice. Especially the word legal, so don't use reserved words in tables or columns. You might have to add double quotes and after that, wherever you reference that table it's going to be pain in the neck. – Anssi Jan 03 '17 at 08:14
  • Even if you pass word like ' ABC1234' in create table command in postgresql , it will be converted to 'abc1234' in Postgre DB and next time when you search table with ' ABC1234', you will run into all sorts of issues.So its better to follow a lowercase naming convention while creating table in postgresql . – Ankur Srivastava Mar 30 '17 at 01:05
  • When specifying a table name, use this syntax: select table."FOOBAR" from table; – vanboom Dec 27 '17 at 15:06
  • Stupid pgsql SQL parser will forced to converted your identifiers to lower-case, so they are not found in pgsql database (case-sensitive). But most databases SQL parsers will do these, so the are intelligent. – Tomex Ou Jun 27 '22 at 17:49
27

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.

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
15

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.

randomness
  • 1,377
  • 1
  • 14
  • 21
3

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))";
Tugay ÜNER
  • 177
  • 1
  • 5
  • 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 Jun 27 '22 at 17:40