Fixing sql for postgres compatibility.