A full implementation of the ab initio density functional plus dynamical mean field theory (DFT+DMFT) formalism to perform total energy calculations and structural relaxations is proposed and implemented. The method is applied to the structural and metal-insulator transitions of the rare earth nickelate perovskites as a function of rare earth ion, pressure, and temperature. In contrast to previous DFT and DFT+U theories, the present method accounts for the experimentally observed structure of LaNiO3 and the insulating nature of the other perovskites, and quantitatively reproduces the metal-insulator and structural phase diagram in the plane of pressure and rare earth element. The temperature dependence of the energetics of the phase transformation indicates that the thermal transition is driven by phonon entropy effects.