← Back to PostgreSQL, page 1

postgresql-0012 · Easy fresher

Why does a table created as CREATE TABLE "UserAccounts" fail when queried as UserAccounts?

PostgreSQL folds unquoted identifiers to lower case, so UserAccounts means useraccounts. A name created with double quotes keeps its exact case and must always be quoted the same way. Use lower-case snake_case names to avoid quoting altogether.

Full answer

Tables and rows used here:

sql
CREATE TABLE "UserAccounts" ("UserName" text);
INSERT INTO "UserAccounts" VALUES ('asha');
sql
SELECT UserName FROM UserAccounts;

Result:

text
ERROR:  relation "useraccounts" does not exist
sql
SELECT "UserName" FROM "UserAccounts";

Output:

text
 UserName
----------
 asha
(1 row)
sql
ALTER TABLE "UserAccounts" RENAME TO user_accounts; ALTER TABLE user_accounts RENAME COLUMN "UserName" TO user_name; SELECT user_name FROM USER_ACCOUNTS;

PostgreSQL returns:

text
 user_name
-----------
 asha
(1 row)

The unquoted query looks for a table called useraccounts, which does not exist, so it fails with 'relation "useraccounts" does not exist'; the lower-case name in the message is the clue. Quoting both names exactly works. Renaming to snake_case fixes it for good, and then any capitalisation works unquoted, as the final query with upper-case letters shows. ORMs and migration tools that quote identifiers can create mixed-case names without anyone noticing, so check generated DDL.

sql
CREATE TABLE "UserAccounts" ("UserName" text);
INSERT INTO "UserAccounts" VALUES ('asha');

SELECT UserName FROM UserAccounts;

SELECT "UserName" FROM "UserAccounts";

ALTER TABLE "UserAccounts" RENAME TO user_accounts; ALTER TABLE user_accounts RENAME COLUMN "UserName" TO user_name; SELECT user_name FROM USER_ACCOUNTS;

Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.

Key points

  • Unquoted names fold to lower case
  • Quoted names are case-sensitive
  • Error messages show the folded name
  • Use snake_case names
  • Check ORM-generated DDL

Common mistakes

  • Creating mixed-case quoted names
  • Quoting inconsistently across queries

Follow-ups

  • How do reserved words interact with quoting?
  • How does MySQL handle identifier case?

Tips

  • Point at the lower-case name in the error