Postgres syntax error at or near "IF"

Not the answer for the OP, but possibly the answer for some who end up here (like myself): If you DECLARE variables within the BEGIN-END block, you will get the same syntax error.

So this is wrong:

DO $$
BEGIN
  DECLARE my_var VARCHAR(50) := 'foo';
  IF some_var IS NULL THEN
     --some logic
  END IF;
END; 
$$ 

This should fix it:

DO $$
DECLARE my_var VARCHAR(50) := 'foo';
BEGIN
  IF some_var IS NULL THEN
     --some logic
  END IF;
END; 
$$ 

IF and other PL/pgSQL features are only available inside PL/pgSQL functions. You need to wrap your code in a function if you want to use IF. If you're using 9.0+ then you can do use DO to write an inline function:

do $$
begin
  -- code goes here
end
$$

If you're using an earlier version of PostgreSQL then you'll have to write a named function which contains your code and then execute that function.