Refactor options into query to make it work with postgres