Fix raw SQL to work with postgresql