How to resolve the error - postgresql relation does not exist?

10.6K    Asked by AndreaBailey in SQL Server , Asked on Sep 29, 2022

I used pg_restore to load my postgres db with a dump file. I connected to my db with my user :

sudo -u arajguru psql dump


select current_user;
 current_user 
--------------
 arajguru
Now I was able to see all the newly created tables:
dump=> dt
               List of relations
 Schema |       Name        | Type  |  Owner   
--------+-------------------+-------+----------
 public | Approvals         | table | arajguru
 public | Approvers         | table | arajguru
 public | Conditions        | table | arajguru
 public | Entities          | table | arajguru
 public | EntityDefinitions | table | arajguru
 public | Projects          | table | arajguru
 public | Rules             | table | arajguru
 public | run_history       | table | arajguru
(8 rows)
But when I try to fire a select * query, it gave me this error:
dump=> select * from Approvals;
ERROR:  relation "approvals" does not exist
LINE 1: select * from Approvals;
What can be the reason for this error? 
Answered by Andrea Bailey

To resolve the error - postgresql relation does not exist - You created your tables with double quotes, and now the names are case sensitive. As documented in the manual "Approvals" and Approvals are two different names. Now that you created the table names in a case sensitive manner, you have to use those dreaded double quotes always.

select * 
from "Approvals";

As a general advice: never use double quotes in SQL.



Your Answer

Answers (2)

The "PostgreSQL relation does not exist" error occurs when trying to access a table, view, index, or sequence that does not exist or is not visible in the current session.

Common Causes and Solutions

1. Table or Relation Does Not Exist

You may be trying to query a table that hasn’t been created.

Solution: Check if the table exists using:

  SELECT * FROM information_schema.tables WHERE table_name = 'your_table';

If the table doesn’t exist, create it:

  CREATE TABLE your_table (id SERIAL PRIMARY KEY, name TEXT);

2. Schema Not Specified or Incorrect

PostgreSQL defaults to the public schema, but your table might be in another schema.

Solution: Include the schema name in the query:

  SELECT * FROM your_schema.your_table;

To set a schema for the session:

  SET search_path TO your_schema;

3. Case Sensitivity Issues

PostgreSQL treats unquoted identifiers as lowercase. If a table was created using double quotes, it is case-sensitive.

Solution: Use double quotes when querying:

  SELECT * FROM "YourTable";  -- Table created with quotes

4. Migration or Incorrect Database Selection

You might be connected to the wrong database.

Solution: Check the current database:

  SELECT current_database();

Switch to the correct database:

  c your_database

Best Practices

✔ Always verify table existence before querying.

✔ Specify the correct schema if not using public.

✔ Avoid using double quotes when creating tables unless necessary.

✔ Ensure you are connected to the correct database.


Would you like help debugging your specific case?

1 Month

The error "relation does not exist" in PostgreSQL typically occurs when trying to query or manipulate a table that doesn't exist in the current schema or database. Here are some steps to resolve this error:


Check Spelling and Case: Ensure that the table name in your SQL query exactly matches the name of the table in the database, including the correct spelling and case sensitivity. PostgreSQL table names are case-sensitive by default.

Verify Schema: If the table exists in a schema other than the default one, make sure to specify the schema name along with the table name when querying it. For example:

SELECT * FROM schema_name.table_name;

Use Qualified Names: Always use fully qualified table names in your queries to avoid ambiguity and ensure that PostgreSQL can locate the correct table. For example:

SELECT * FROM database_name.schema_name.table_name;

Check Permissions: Ensure that the user executing the query has the necessary permissions to access the table. You can grant the required permissions using the GRANT command.

Verify Database Connection: Make sure that you're connected to the correct database where the table exists. If not, reconnect to the appropriate database.

Table Creation: If you're trying to access a table that hasn't been created yet, create it using the CREATE TABLE command before attempting to query or manipulate it.

Check for Renamed or Dropped Tables: If the table was renamed or dropped, update your SQL queries accordingly or restore the table from a backup if necessary.

Database Migration: If you recently migrated your database, ensure that all tables were successfully migrated, and there were no issues with the migration process.

By following these steps and troubleshooting the potential causes of the "relation does not exist" error, you can resolve the issue and successfully access the table in PostgreSQL.


10 Months

Interviews

Parent Categories