Skip to main content
0 reactions

Why PostgreSQL Turns Names Lowercase, and What Double Quotes Do

Card for Why PostgreSQL Turns Names Lowercase: Pets is stored as pets, so Pets, PETS and pets are the same name, while “Toys” in double quotes is stored as Toys and must be quoted every time.
Why PostgreSQL Turns Names Lowercase.

Create a table called Pets in PostgreSQL, and it’s stored as pets. That surprises people who come from other databases, and it’s behind two common errors: relation does not exist and column does not exist. Here’s why PostgreSQL turns names lowercase, and what double quotes change.

Every command and output on this page is real, from PostgreSQL 19 on macOS.

Syntax

SyntaxSQL
name
"Name"
  • name, without quotes: letters, digits, underscores and dollar signs, starting with a letter or an underscore. PostgreSQL turns it into lowercase, so Name, NAME and name are the same name.
  • "Name", in double quotes: any characters, including capitals, spaces and hyphens, kept exactly as typed. You must write it the same way, quotes included, every time.
  • Either way, a name can be up to 63 bytes long.

The rules are in Identifiers and Key Words (opens in a new tab) in the PostgreSQL documentation.

Step 1: See Unquoted Names Turn Lowercase

Connect to your server with psql as the postgres user, as Part 2 of the PostgreSQL Tutorial shows. psql connects you to the postgres database, and its prompt is postgres=#.

This guide uses a test database called casedemo. Create it and connect to it:

postgres=#SQL
CREATE DATABASE casedemo;
OutputPlain text
CREATE DATABASE
postgres=#Plain text
\c casedemo
OutputPlain text
You are now connected to database "casedemo" as user "postgres".

Create a table with capitals in its name and its column, then add a row with INSERT, spelling the names in capitals again:

casedemo=#SQL
CREATE TABLE Pets (Name text);
OutputPlain text
CREATE TABLE
casedemo=#SQL
INSERT INTO PETS (NAME) VALUES ('Rex');
OutputPlain text
INSERT 0 1

List the tables with \dt:

casedemo=#Plain text
\dt
OutputPlain text
          List of tables
 Schema | Name | Type  |  Owner
--------+------+-------+----------
 public | pets | table | postgres
(1 row)

PostgreSQL stored the table as pets and its column as name. Because names without quotes are always turned into lowercase, Pets, PETS and pets all mean the same table:

casedemo=#SQL
SELECT name FROM pets;
OutputPlain text
 name
------
 Rex
(1 row)

Step 2: See Double Quotes Keep Capitals

casedemo=#SQL
CREATE TABLE "Toys" ("Name" text);
OutputPlain text
CREATE TABLE
casedemo=#Plain text
\dt
OutputPlain text
          List of tables
 Schema | Name | Type  |  Owner
--------+------+-------+----------
 public | Toys | table | postgres
 public | pets | table | postgres
(2 rows)

Now there’s a table called Toys, with a capital T, next to pets.

Step 3: Quote the Name Every Time

Leave out the quotes, and Toys becomes toys, which doesn’t exist:

casedemo=#SQL
SELECT * FROM Toys;
OutputPlain text
ERROR:  relation "toys" does not exist
LINE 1: SELECT * FROM Toys;
                      ^

The same happens to the column:

casedemo=#SQL
SELECT Name FROM "Toys";
OutputPlain text
ERROR:  column "name" does not exist
LINE 1: SELECT Name FROM "Toys";
               ^
HINT:  Perhaps you meant to reference the column "Toys.Name".

PostgreSQL even suggests the fix in its HINT. Quote both names, with the same capitals:

casedemo=#SQL
SELECT "Name" FROM "Toys";
OutputPlain text
 Name
------
(0 rows)

Why PostgreSQL Does This

The SQL standard says names without quotes should be turned into uppercase. PostgreSQL turns them into lowercase instead, as its documentation (opens in a new tab) notes. Either way, its advice is the same: always quote a particular name, or never quote it.

Tip: The simplest habit is never to quote: write every name in lowercase, with underscores between words, such as order_items.

Clean Up

Connect to another database, then drop the test database:

casedemo=#Plain text
\c postgres
OutputPlain text
You are now connected to database "postgres" as user "postgres".
postgres=#SQL
DROP DATABASE casedemo;
OutputPlain text
DROP DATABASE

Common Mistakes

MistakeWhat happensDo this instead
Creating a name with quotes, then using it without themERROR: relation "toys" does not existUse the quotes every time, or name the table in lowercase without quotes.
Capitals in a column created with quotesERROR: column "name" does not existQuote the column, with the same capitals: "Name".

FAQ

Does this apply to database names too?

Yes. A database name follows the same rules, so CREATE DATABASE MyShop; creates a database called myshop. Part 4 of the PostgreSQL Tutorial shows it with its bookstore database.

Are keywords such as SELECT case-sensitive?

No. SELECT, select and Select are the same. Writing keywords in capitals is only a convention that makes SQL easier to read.

Conclusion

PostgreSQL turns names without quotes into lowercase, and keeps names in double quotes exactly as typed. Stick to lowercase names without quotes, and these errors never come up.

Additional Resources

Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.

0 comments