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

9.5K    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

Answer (1)

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.


6 Months

Interviews

Parent Categories