Site icon JD Bots

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

Syntax

name
"Name"

The rules are in Identifiers and Key Words 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=#

CREATE DATABASE casedemo;

Output

CREATE DATABASE

postgres=#

\c casedemo

Output

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=#

CREATE TABLE Pets (Name text);

Output

CREATE TABLE

casedemo=#

INSERT INTO PETS (NAME) VALUES ('Rex');

Output

INSERT 0 1

List the tables with \dt:

casedemo=#

\dt

Output

          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=#

SELECT name FROM pets;

Output

 name
------
 Rex
(1 row)

Step 2: See Double Quotes Keep Capitals

casedemo=#

CREATE TABLE "Toys" ("Name" text);

Output

CREATE TABLE

casedemo=#

\dt

Output

          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=#

SELECT * FROM Toys;

Output

ERROR:  relation "toys" does not exist
LINE 1: SELECT * FROM Toys;
                      ^

The same happens to the column:

casedemo=#

SELECT Name FROM "Toys";

Output

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=#

SELECT "Name" FROM "Toys";

Output

 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 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=#

\c postgres

Output

You are now connected to database "postgres" as user "postgres".

postgres=#

DROP DATABASE casedemo;

Output

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

Related

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

Exit mobile version