title: Execute select statement IF table exists
body: <p>FINAL PURPOSE: <strong>Generate a query that, if a table exists, execute a statement for that table</strong></p>
  
  <p>I'm trying to execute a PSQL (9.6) statement only if a certain table exists in the schema, but it always responds with a syntax error each time I try to use conditional <code>IF</code>.</p>
  
  <p>My query is something like...</p>
  
  <pre><code>IF EXISTS(SELECT 1 FROM information_schema.tables WHERE table_name = 'users') THEN
    SELECT * FROM users;
  END IF;
  </code></pre>
  
  <p>And the output is...</p>
  
  <pre><code>ERROR:  syntax error at or near "IF"
  LINE 1: IF EXISTS(SELECT 1 FROM information_schema.tables WHERE tabl...
          ^
  </code></pre>
  
  <p><em>There is no more code than this</em>. All alternatives that I tried failed.</p>