Fix sql query for postgres