How to Fix “relation does not exist” Error in Postgres

PostgreSQL is an RDBM system that is used for creating databases that store data in tabular form. In PostgreSQL, tables are also referred to as relations. The relations must be named correctly, otherwise, users may encounter an error message. Moreover, the table names are essential for accessing data from PostgreSQL, as they help us retrieve information from the tables.

This guide will explain how to fix the “relation does not exist” error in the PostgreSQL database.

How to Fix the “relation does not exist” Error in Postgres?

The Error “relation does not exist” occurs in the PostgreSQL database when the user makes mistakes while calling the table name. The error message appears if the user has made a spelling mistake, uses the wrong spelling convention, etc. It can also give an error message if the relation is unavailable in the database. To fix the “relation does not exist” error in PostgreSQL databases, follow the simple steps mentioned below:

Prerequisite

To use the PostgreSQL databases and tables, it is required to connect to its server using the following command:

psql --username=postgres

Running the above command will prompt the user to enter the Master password for the PostgreSQL database:

img

After connecting to the Postgres user in the PostgreSQL server, head into the database by using the following command:

\c JOIN

Executing the above command will direct the user inside the selected database:

img

Use the following command to get the list of all the tables available in the database with their names:

\dt
img

Reason 1: Spelling Mistake

A common mistake a user can make while calling the table in the PostgreSQL database is typing the wrong spelling as the following code block contains:

SELECT * FROM Cars;

Running the above code displayed the error that the “relation “cars” does not exist”:

img

The spelling of the select table is “Car” not “Cars” so the user needs to write the exact spelling of the relations as displayed in the following code block:

SELECT * FROM "Car";
img

Reason 2: Table is Not Available/Exist

Access the data from the vehicles table using the following query:

SELECT * FROM Vehicles;

Running the above code has displayed the “relation “vehicles” does not exist” error:

img

Use the following command to get the list of all the tables available in the database:

\dt

The following are the tables available in the PostgreSQL database so the user can access only these tables:

img

Use the following command to access the “employee” table from the PostgreSQL database:

SELECT * FROM employee;
img

That’s all about solving the stated error in PostgreSQL when the query cant fetch a table from the database.

Conclusion

To fix the “relation does not exist” error in the PostgreSQL database, simply connect to the PostgreSQL server and head into the database. After that, check all the tables/relations available on the database by using the “\dt” command to get the names of all the tables. After getting the list of all the tables, simply use the correct spelling and case convention to call the table. This guide has explained the process to solve the “relation does not exist” error in PostgreSQL databases.