2

In PostgreSQL I try to check if table exist:

SELECT EXISTS (SELECT * FROM table_name);

And it throwing an error. How can I check if table already exists so the result will be boolean? Because currently I can achieve the same with try-catch (enters to catch if not exist) instead of if-else on the result...

Thanks,

1

1 Answer 1

4

Either of these should work, though depending on how your permissions are set up you may not have access to the tables:

SELECT EXISTS (SELECT relname FROM pg_class WHERE relname = 'table_name');

SELECT EXISTS (SELECT table_name FROM information_schema.tables WHERE table_name = 'table_name');

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.