Are PostgreSQL column names case-sensitive?

SqlPostgresqlIdentifierCase SensitiveCase Insensitive

Sql Problem Overview


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?

Sql Solutions


Solution 1 - Sql

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 not needed.

Solution 2 - Sql

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.

Solution 3 - Sql

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.

Solution 4 - Sql

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;

Solution 5 - Sql

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

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
Question5122014009View Question on Stackoverflow
Solution 1 - SqlErwin BrandstetterView Answer on Stackoverflow
Solution 2 - SqlEugene YarmashView Answer on Stackoverflow
Solution 3 - SqlrandomnessView Answer on Stackoverflow
Solution 4 - SqlCarlos Ernesto Lizarazo SierraView Answer on Stackoverflow
Solution 5 - SqlTugay ÜNERView Answer on Stackoverflow