fixed bugs with postgres support.
code cleanup.

Change-Id: Id2960b94f6f813a018b555af95ff30b0454607f3