Fix bug on postgress query