Completely refactored the postgresql side.