How to Resolve Postgres "column does not exist" After a Schema Change

Postgres raises ERROR: column "active" does not exist when a query names a column that the table no longer has - most often because a migration renamed it. Check the real columns with \d table_name, change every query to the new name, and make sure your API logs the database error instead of returning an empty result.

Start this project - freeLive cloud workspace in your browser · lands on your portfolio
Backend Engineerpythonflaskpostgresql

What the error means

Postgres checks every column name in a query against the table definition before it runs anything. If one name is not there, the whole statement fails:

ERROR:  column "active" does not exist
LINE 1: SELECT id, name, email FROM users WHERE active = true
                                                ^
HINT:  Perhaps you meant to reference the column "users.is_active".

The caret shows the exact name Postgres could not find, and the HINT line often suggests the closest real column. The usual causes are:

Why the API returns an empty list instead of an error

This bug is hard to see when the code catches the database error and returns a default value:

@app.get("/users")
def list_users():
    try:
        cur.execute(
            "SELECT id, name, email FROM users WHERE active = true ORDER BY name"
        )
        rows = cur.fetchall()
    except psycopg2.Error:
        rows = []          # the real error is lost here
    return jsonify({"count": len(rows), "users": rows})

The client gets {"count": 0, "users": []} with status 200. Nothing looks broken, but no user is ever returned. At minimum, log the exception so the real message reaches your logs:

    except psycopg2.Error as e:
        app.logger.error("list_users query failed: %s", e)
        return jsonify({"error": "database error"}), 500

An empty list and a failed query are different results. Returning a 500 makes the failure visible to monitoring and to the client.

Step 1: look at the real table definition

Open psql and describe the table:

psql -h localhost -U postgres -d app -c "\d users"
     Column   |  Type   | Nullable
--------------+---------+----------
 id           | integer | not null
 name         | text    | not null
 email        | text    | not null
 role         | text    |
 department   | text    |
 is_active    | boolean | not null

You can get the same list with plain SQL, which also works from code:

SELECT column_name, data_type
FROM information_schema.columns
WHERE table_name = 'users'
ORDER BY ordinal_position;

Step 2: update every query that uses the old name

Search the code for the old column name. A renamed column is rarely used in only one place - here it appears in both the filtered and the unfiltered query:

grep -rn "active = true" app.py
if role:
    cur.execute(
        "SELECT id, name, email, role, department FROM users "
        "WHERE is_active = true AND role = %s ORDER BY name",
        (role,),
    )
else:
    cur.execute(
        "SELECT id, name, email, role, department FROM users "
        "WHERE is_active = true ORDER BY name"
    )

Be careful with a plain search for the word active: it also matches is_active, inactive and strings in comments. Search for the full expression, or review each match.

Step 3: confirm with real requests

curl -s localhost:8000/users | python3 -c "import sys,json; print(json.load(sys.stdin)['count'])"
curl -s "localhost:8000/users?role=engineer" | python3 -c "import sys,json; print(json.load(sys.stdin)['count'])"

The first number should match the count of active rows in the table:

SELECT count(*) FROM users WHERE is_active = true;

How to stop this from happening again

Do this project yourself. It's ready in a live cloud workspace in your browser - when you finish, it lands on your portfolio.

Start this project - free

What you'll practice

FAQ

Why does Postgres say a column does not exist when I can see it in the table?

The most common reason is case. Postgres folds unquoted names to lowercase, so if the column was created as "isActive" with double quotes, you must write "isActive" with quotes every time. Also check that you are connected to the right database and schema.

How do I list the columns of a table in Postgres?

In psql, run \d table_name. From SQL or application code, query information_schema.columns with WHERE table_name = 'your_table'.

How do I rename a column in Postgres?

Run ALTER TABLE users RENAME COLUMN active TO is_active;. The rename is fast, but every query that uses the old name fails right after it, so update the application code in the same release.

Why did my API return an empty list instead of an error?

The code probably catches the database exception and returns a default value. The query fails, the except block sets the result to an empty list, and the client sees status 200. Log the exception and return a 500 so the failure is visible.

Keep learning

Run a Zero-Downtime Postgres MigrationBackend projectAdd a NOT NULL Column With No DowntimeBackend projectReturn the Correct HTTP Status CodeBackend projectBackend roadmapStep by step to hiredBackend interview questionsSTAR answersAll Backend projectsProjects hub

Do this project yourself. It's ready in a live cloud workspace in your browser - when you finish, it lands on your portfolio.

Start this project - free
Start this project - free