Completed first version with postgresql support. Some extra fixing needed