fixing postgres queries