How to check if a table exists in PostgreSQL Schema or not

In this article, we are going to check whether a table exists in PostgreSQL schema or not. Following queries are used in this article. If table exists then output will be ‘t’ otherwise ‘f’.

Query to check tables exists or not in PostgreSQL Schema or not 1:

SELECT EXISTS( SELECT * FROM information_schema.tables WHERE table_schema = 'schemaname' AND table_name = 'tablename' );

Query to check tables exists or not in PostgreSQL Schema or not 2:

SELECT EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = 'schemaname' AND table_name = 'tablename' );

Query 1 Examples:

SELECT EXISTS( SELECT * FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'student' );

How to check if a table exists in PostgreSQL Schema or not

SELECT EXISTS( SELECT * FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'class' );

Query 2 Examples:

SELECT EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' and table_name='student');

SELECT EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' and table_name='class');