In this paper we study the optimal management of an aggregated pension fund of defined benefit type, in the presence of a stochastic interest rate. We suppose that the sponsor can invest in a savings account, in a risky stock and in a bond with the aim of minimizing deviations of the unfunded actuarial liability from zero along a finite time horizon. We solve the problem by means of optimal stochastic control techniques and analyze the influence on the optimal solution of some of the parameters involved in the model.