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,

michael
  • 3,835
  • 14
  • 53
  • 90

1 Answers1

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');