PostgreSQL is a widely used relational database system. It is very flexible. But when you use these relational databases, sometimes you may get confused about whether there is a column name case sensitivity or not. Understanding the case sensitivity of PostgreSQL and knowing the approaches to handle it is very important to avoid any errors in queries. In this blog, let’s learn about PostgreSQL case sensitivity in detail with practical examples.
Table of Contents:
What is case-sensitive?
Usually, when we query commands to the database in SQL, it may return a case-sensitive error. The lowercase and uppercase of the letter are considered distinct in Compilers. This means the “table_name” and “Table_Name” are not different as per the ASCII values. They both possess different identities. So when there is a case-sensitive system, they would be treated differently, but in a case-insensitive system, they would be treated the same.
Example:
“SalesData” is not equal to “salesdata” in a case-sensitive system.
“SalesData” is equal to “salesdata” in a case-insensitive system.
What is Case-sensitive in PostgreSQL?
PostgreSQL has a default setting that converts the unquoted identifiers like table and column names. But in PostgreSQL, if you use identifiers within a double quote (“ ”), they will be treated as the same identifier as mentioned in the quotes. PostgreSQL will convert the unquoted identifiers into lowercase letters during execution.
For example: Let’s create a people table:
CREATE TABLE people (UserName TEXT);
Here, inside the PostgreSQL server, the “UserName” has been converted into “username” as it is unquoted in the query.
Internally it will be treated as:
CREATE TABLE users (username TEXT);
Inserting the values:
INSERT INTO people (username) VALUES ('Shetty');
INSERT INTO people (username) VALUES ('Raj');
------Display with lowercase column names
SELECT username FROM people;
Output:
This will be the table that was displayed after querying and converting the column name.
If there is an error:
CREATE TABLE people (UserName TEXT);
INSERT INTO people (username) VALUES ('Shetty');
INSERT INTO people (username) VALUES ('Raj');
------Display with lowercase column names
SELECT UserName FROM users;
This is how the error will be displayed if the identifiers are not within double quotes. As there are no double quotes in “users” the compiler can’t find the value for it.
Case-sensitive column names in PostgreSQL
PostgreSQL is case-sensitive when it comes to column names. To get the proper output in the server without any case-sensitive errors, you can use double quotes (“ ”) to overcome this issue. To get the same name as the column name, you can mention it in between double quotes.
Example:
CREATE TABLE users ("UserName" TEXT);
INSERT INTO users ("UserName") VALUES ('Tarun');
INSERT INTO users ("UserName") VALUES ('Priya');
SELECT "UserName" FROM users;
Output:
Explanation: Here, the PostgreSQL server converted the “UserName” uppercase into “username” lowercase as it is in double quotes.
Practical Examples of Column Names in PostgreSQL
Here is an example that will show you how PostgreSQL behaves for the quoted and unquoted identifiers in the server.
Case 1: Using Quoted Column Names in PostgreSQL
CREATE TABLE Buses (
"BusID" SERIAL PRIMARY KEY,
"FirstName" TEXT,
"LastName" TEXT,
"EmailAddress" TEXT
);
INSERT INTO Buses ("FirstName", "LastName", "EmailAddress")
VALUES ('Kia', 'Advain', '[email protected]');
INSERT INTO Buses ("FirstName", "LastName", "EmailAddress")
VALUES ('Karan', 'Johar', '[email protected]');
SELECT "FirstName", "LastName", "EmailAddress" FROM Buses;
Output:
Explanation: Here, the PostgreSQL server retained the original column name without changing the uppercase as they are presented inside the double quotes. But this will behave differently and raise an error sometimes when the compiler does not understand the query.
Case 2: Using Unquoted Column Names in PostgreSQL
Example:
CREATE TABLE users (
UserName TEXT, -- Not quoted
EmailAddress TEXT
);
Output:
Explanation: Here, the server will raise an error in executing the query as it does not recognize the uppercase column name.
Best Practices to Avoid Issues
- Use lowercase in the query whenever necessary, as quoted identifiers may raise issues with unexpected output and errors.
- When you use double quotes in queries, make sure that you use those quotes throughout the queries. Before executing, make sure that your column names are correct.
- Use underscores for the column name to get better readability.
- Make sure that you are not using reserved keywords in the query. If you are using it, use it within double quotes or rename it.
Key Differences between Case-Insensitive and Case-Sensitive
Feature | Case-Insensitive (Unquoted) | Case-Sensitive (Quoted) |
Definition | The queries were created without quotes. | The query will be executed with double quotes (“ ”). |
In PostgreSQL server it behaves as | The unquoted identifiers will be converted into lowercase while querying. | The query or column name will be executed exactly as it is mentioned within quotes. |
Query Behavior | The conversion of uppercase to lowercase must match with the correct column name. | The column name must match with the quoted column name. |
Syntax | CREATE TABLE users (UserName TEXT); | CREATE TABLE users (“UserName” TEXT); |
Compiler internally consider as | username | “UserName” |
Proper query | SELECT username FROM users; | SELECT “UserName” FROM users; |
Error query | SELECT UserName FROM users; | SELECT username FROM users; |
Error Message from compiler | ERROR: column “UserName” does not exist | ERROR: column “username” does not exist |
Troubleshooting Case Sensitivity Issues in PostgreSQL
- Check the system views before querying to know whether the column name is stored in lowercase or uppercase.
- Verify the column, whether it was created with quotes or without quotes.
- If with quotes, ensure that queries are matching with the exact name.
- If without quotes, create the queries in lowercase.
- Check the error message saying, “column does not exist.” if it popped in the PostgreSQL server, then the query provided has a case-sensitive issue. Check for the identifier, then correct the case.
- Consider using double quotes if you want to retain the same case column name or use special characters in the query.
- Use an underscore and lowercase in the query for efficient results, like first_name instead of using FirstName.
- Drop a table if necessary and if it raises a case-sensitive issue. Create a new table in lowercase to avoid the issue.
Comparison of Case Sensitivity in Different SQL Servers
Feature | PostgreSQL Server | MySQL Server | SQL Server |
Uppercase (unquoted identifiers) | In this server, it will convert them into lowercase. | MySQL server will also convert it into lowercase. | It will retain the value, but consider them as an insensitive case. |
Lowercase (Quoted Identifiers) | It is case-sensitive, so it must match with lowercase. | It is also case-sensitive, so the cases must match. | In SQL Server, there is case sensitivity in some queries. |
Condition for table names | It is lowercase by default | It is case-sensitive when used on Linux and insensitive on MAC or Windows. | It is case-sensitive by default. |
Condition for column names | For column names, it is case-sensitive. | It is case-insensitive for column names. | It is case-insensitive by default for column names. |
String Comparisons | It only compares lowercase strings, as it is the default. | MySQL has a standard that it is case-insensitive | SQL Server has a standard that it is case-insensitive |
How is it sorted? | It is sorted by column or database | It will sort column by column | It sorts tables and columns in a database. |
Best Practice | Use lowercase identifiers for best results. | Depending on OS-dependent cases must be avoided. | Always use case-insensitive unless you need to sort the table. |
Conclusion
In conclusion, the column names in PostgreSQL are case-sensitive. You can enclose it using double quotes, but make sure that the query is exactly as it was created. For unquoted column names, it will automatically convert them into lowercase. To avoid any unnecessary errors, use lowercase identifiers throughout the query. Quoted column names will cause unexpected errors if they’re not handled correctly. Understanding PostgreSQL’s sensitivity behavior, we can write error-free queries in the application.
Take your skills to the next level, by enrolling in the SQL Training Course today and gain hands-on experience. Also, prepare for job interviews with our SQL interview questions, prepared by industry experts.
Are PostgreSQL column names case-sensitive – FAQs
1. Is the PostgreSQL column name case-sensitive?
Yes, if enclosed in double quotes; otherwise, they are converted to lowercase.
2. Are column names case-sensitive?
Yes, when using double quotes; otherwise, they are treated as lowercase.
3. What are the rules for column names in PostgreSQL?
Unquoted column names are converted to lowercase, while quoted names retain their exact case.
4. What type of column is case-insensitive in Postgres?
Unquoted column names are case-insensitive since they are automatically converted to lowercase.
5. How do you check if a column is case-sensitive in SQL?
Query system catalogs pg_columns or information_schema.columns to check the exact column name.