Are PostgreSQL column names case-sensitive?
In PostgreSQL, unquoted column names are lowercase. Use double quotes for case sensitivity:
-
Without quotes (treats as lowercase):
-
With quotes (keeps original case):
To dodge the confusion, don't use quotes altogether, or remember to always quote with the exact case.
Unmasking PostgreSQL's treatment of column names
Living in the lowercase world
When you don't use quotes, PostgreSQL:
-
Transforms identifiers to lowercase:
-
Keeps identifiers and strings completely separate:
Embracing case sensitivity
When you do use double quotes:
-
PostgreSQL follows your exact case and characters:
-
Requires consistent quoting in every reference, which might turn into an exercise of accurate memory!
Nailing it: The recommended way to name columns
Sticking to lowercase
Using only lower-case names:
-
Eliminates the need to type quotes:
-
Makes your SQL queries more readable and less error-prone.
Steering clear of reserved words
Staying away from SQL reserved words:
-
Avoids potential syntax conflicts:
-
Promotes use of more descriptive and unambiguous identifiers.
Application & portability considerations
Impact on application development
Maintaining consistency: Inconsistencies can lead to errors in application development, especially with ORMs:
Handling database migrations
Transferring or transitioning databases can sometimes turn into a puzzle game. To modify column names to lowercase:
Understanding SQL standards
PostgreSQL is like the rebellious teenager of SQL databases:
- SQL is set to default fold unquoted names to upper case.
- But PostgreSQL, being the cool kid, folds to lower case.
Dealing with literal constants vs. identifiers
-
Wrap string literals or constants in single quotes:
-
Reserve double quotes for exact-case column names:
Was this article helpful?