Error
Error Code: 42704

PostgreSQL Error 42704: undefined object

📦 PostgreSQL
📋

Description

This error indicates that your SQL statement references a database object (like a table, view, column, or function) that PostgreSQL cannot find or access. It often points to a typo, a missing object, or a schema resolution issue.
💬

Error Message

undefined object
🔍

Known Causes

4 known causes
⚠️
Typo in Object Name
The most common cause is a simple misspelling of a table, column, view, or function name in your SQL query.
⚠️
Object Does Not Exist
The referenced database object was never created, has been dropped, or you are connected to the wrong database or schema.
⚠️
Incorrect Schema or Search Path
The object exists but is in a different schema, and your current `search_path` does not include it, or you haven't fully qualified the name.
⚠️
Case Sensitivity Mismatch
If an object was created with mixed-case or uppercase using double quotes, referencing it without quotes or with incorrect casing will lead to this error.
🛠️

Solutions

4 solutions available

1. Verify Object Name and Schema easy

Double-check that the object name and its schema are spelled correctly and exist in the current database.

1
Carefully review the SQL query that produced the error. Pay close attention to the name of the table, function, sequence, or other object being referenced.
Example: SELECT * FROM my_tabl LIMIT 10;  -- Typo in 'my_tabl'
2
If the object is in a specific schema (e.g., `myschema.mytable`), ensure the schema name is also correct and that the object exists within that schema.
Example: SELECT * FROM myschema.mytable;  -- 'myschema' or 'mytable' might be misspelled or not exist
3
Connect to your PostgreSQL database using `psql` or a GUI tool (like pgAdmin, DBeaver).
psql -U your_user -d your_database -h your_host
4
List all objects in the relevant schema or across all schemas to confirm the object's existence and spelling.
For a specific schema:
\dn
\dt your_schema.*

To search all schemas:
\dt
\df
\dv
\ds
\dc
5
If you find a typo, correct the object name in your SQL query and re-execute it.
Example: SELECT * FROM my_table LIMIT 10; -- Corrected spelling

2. Check Object Existence in Search Path medium

Ensure the object is discoverable by PostgreSQL based on the current `search_path` setting.

1
Understand that PostgreSQL uses a `search_path` to determine where to look for unqualified object names (names without a schema prefix). If an object is not in any schema listed in the `search_path`, you'll get this error.
2
View the current `search_path` for your session.
SHOW search_path;
3
If the object is in a schema that is not in the `search_path`, you have two primary options:
4
Option A: Qualify the object name with its schema in your query.
Example: If `search_path` is '$user',public and your table is in `my_schema`, use:
SELECT * FROM my_schema.my_table;
5
Option B: Temporarily or permanently add the schema to your `search_path`.
Temporarily for the current session:
SET search_path TO my_schema, public;

Permanently for a specific user (requires superuser privileges):
ALTER USER your_user SET search_path TO my_schema, public;

Permanently for a specific database (requires superuser privileges):
ALTER DATABASE your_database SET search_path TO my_schema, public;
6
Re-execute your query after adjusting the `search_path` or qualifying the object name.

3. Confirm Object Permissions medium

Verify that the current user has the necessary privileges to access the object.

1
The error might occur if the object exists but the user executing the query lacks the required permissions (e.g., `SELECT` on a table, `EXECUTE` on a function).
2
Connect to your PostgreSQL database as a user with sufficient privileges (e.g., a superuser or the owner of the object).
psql -U superuser_or_owner -d your_database -h your_host
3
Check the privileges granted on the specific object. Replace `your_schema.your_object` with the actual object name and schema.
For tables:

	describe your_schema.your_table;

For functions:

		describe function your_schema.your_function(arg_types);

Alternatively, query the system catalogs:

SELECT grantee, privilege_type FROM information_schema.role_table_grants WHERE table_schema = 'your_schema' AND table_name = 'your_table';

SELECT grantee, privilege_type FROM information_schema.role_usage_grants WHERE object_schema = 'your_schema' AND object_name = 'your_function';
4
If the current user does not have the necessary privileges, grant them. Replace `your_user`, `your_schema.your_object`, and `privilege_type` accordingly.
Granting SELECT on a table:
GRANT SELECT ON your_schema.your_table TO your_user;

Granting EXECUTE on a function:
GRANT EXECUTE ON FUNCTION your_schema.your_function(arg_types) TO your_user;

If you need to grant for all tables in a schema (use with caution):
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA your_schema TO your_user;

If you need to grant for all functions in a schema (use with caution):
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA your_schema TO your_user;
5
Log in as the user who encountered the error and re-run the query.

4. Verify Object Type and Usage easy

Ensure you are referencing the object with the correct syntax for its type (e.g., table, function, sequence).

1
The error can occur if you are trying to use an object in a way that doesn't match its type. For example, trying to `SELECT` from a function, or calling a table like a function.
2
Identify the type of object you are trying to access. Is it a table, view, function, sequence, or something else?
3
Use the appropriate PostgreSQL commands to confirm the object's type.
To list tables:
\dt

To list views:
\dv

To list functions:
\df

To list sequences:
\ds

To list data types:
\dT
4
Review your SQL query and ensure the syntax matches the object type. For example:
Correct for a table:
SELECT * FROM your_table;

Correct for a function:
SELECT * FROM your_function(arg1, arg2);

Correct for a sequence:
SELECT nextval('your_sequence');
5
If you are trying to use a table as if it were a function, or vice-versa, correct the query syntax.
🔗

Related Errors

5 related errors