We examine the optimal policy response to an exogenously given demographic shock. Such a shock affects negatively the financing of retirement pensions, and we use optimal fiscal policy in order to determine the optimal strategy of the social security administration. Our approach provides specific policy responses in an environment that guarantees the financial sustainability of existing retirement pensions. At the same time, pensions will be financed in a way that by construction generates no welfare losses for any of the cohorts in our economy. In contrast to existing literature we endogenously determine optimal policies rather than exploring implications of exogenously given policies. Our results show 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.