We examine the optimal policy response to a transitory demographic shock that affects negatively the financing of retirement pensions. In contrast to existing literature, we endogenously determine optimal policies rather than exploring implications of exogenous parametric policies. Our approach identifies optimal strategies of the social security administration to guarantee the financial sustainability of existing retirement pensions in a Pareto improving way. Hence, no cohort will pay the cost of the demographic shock. We find that the optimal strategy is based in the following ingredients: elimination of compulsory retirement, a change in the structure of labor income taxation and a temporary increase in the level of government debt.