Explain Codes LogoExplain Codes Logo

Postgresql "Column does not exist" but it actually does

sql
best-practices
sql-syntax
database-structure
Nikita BarsukovbyNikita Barsukov·Nov 27, 2024
TLDR

The "Column does not exist" error in PostgreSQL is likely due to case sensitivity or mispelled identifiers. Wrap the case-sensitive column name in double quotes ":

SELECT "CaseSensitiveColumn" FROM your_table;

Ensure no misspelling and verify the correct schema use if the column indeed exists.

Case sensitivity: nasty beast or helpful creature?

PostgreSQL identifiers, such as column names, are case-insensitive by default. However, the scene changes dramatically when double quotes enter the picture:

-- These are besties: SELECT column_name FROM my_table; SELECT COLUMN_NAME FROM my_table; SELECT Column_Name FROM my_table; -- These guys barely talk: SELECT "column_name" FROM my_table; --"Hi, nice to meet you!" SELECT "COLUMN_NAME" FROM my_table; --"Who are you again?"

Try using lowercase for column names to avoid case sensitivity. Unquoted names are stored in lowercase by default.

Special characters and whitespaces: wanted or not?

White spaces or special characters in column names require double quotes:

CREATE TABLE my_table ( "A Column" text, "Another-Column" integer ); SELECT "A Column", "Another-Column" FROM my_table;

Hidden characters or whitespaces? Those sneaky troublemakers could be in your column names! Paste the name from your DB schema to catch them red-handed.

What table are we eating at?

Confirm you're dining at the right table and serving from the correct schema:

--Waiter! I'd like to order "column_name" from "schema_name"."table_name". Table for one, please. SELECT "column_name" FROM "schema_name"."table_name";

Mismatched application model and database structure can leave a sour taste in your mouth. Keep your schema migrations fresh from the kitchen!

SQL syntax: secret spice or bitter herb?

From column names to SQL syntax and operators:

  • Single quotes for string literals—no doubles! Cheating on doubles with singles, staying string to our rule.
  • Comparison operators must match the column data type. "Apples to apples, oranges to oranges."

Testing and Troubleshooting: keep or fold?

Always bet on testing your query directly in the PostgreSQL console or psql tool. Java application? Turn on the log-light to see what's being dished out to the DB:

--"Waiter! There's a typo in my soup." LOG: SELECT "yourClumn" FROM your_table;

Quoting the unquoteables

Reserved words for column names? It's fraught with peril! Always keep them safe in quotes:

-- Yes, we have "user" on the menu. And yes, it comes quoted. SELECT "user" FROM my_table;

Changing column names? Here's how you give your column a new identity:

-- Goodbye, Old Column. Welcome, new_column! New name, fresh start. ALTER TABLE my_table RENAME COLUMN "Old Column" TO new_column;

Step-by-step elimination

Diagnosing the problem

Before making drastic changes, complete a thorough diagnosis:

  • Run \d table_name in psql to reveal the truth.
  • Toggle \x command for clarity's sake.
  • Cross-verify column data types—incorrect types can wreak similar havoc in join or comparison queries.

Digging Deeper with PostgreSQL Functions

Meet the functions at your disposal:

SELECT * FROM pg_catalog.pg_attribute WHERE attrelid = 'my_table'::regclass;

This query lists the actual columns of my_table stored in the system catalog.

Controlled migrations

For renaming or changing schema:

  • Have orderly migration scripts with definite up and down paths.
  • After running migrations, schema diff tool will confirm the changes.
  • Tools like Alembic or Flyway can ensure error-free schema migrations.

A developer's golden rules

Follow these simple rules:

  • Keep sync between application and database schema.
  • Stick to naming conventions: lowercase with underscores is friendly for PostgreSQL.
  • Avoid altering the schema manually and breaking migration scripts' trust.