Earth's global magnetic field arises from vigorous convection within the liquid outer core. Palaeomagnetic evidence reveals that the geodynamo has operated for at least 3.4 billion years, which places constraints on Earth's formation and evolution. Available power sources in standard models include compositional convection (driven by the solidifying inner core's expulsion of light elements), thermal convection (from slow cooling), and perhaps heat from the decay of radioactive isotopes. However, recent first-principles calculations and diamond-anvil cell experiments indicate that the thermal conductivity of iron is two or three times larger than typically assumed in these models. This presents a problem: a large increase in the conductive heat flux along the adiabat (due to the higher conductivity of iron) implies that the inner core is young (less than one billion years old), but thermal convection and radiogenic heating alone may not have been able to sustain the geodynamo during earlier epochs. Here we show that the precipitation of magnesium-bearing minerals from the core could have served as an alternative power source. Equilibration at high temperatures in the aftermath of giant impacts allows a small amount of magnesium (one or two weight per cent) to partition into the core while still producing the observed abundances of siderophile elements in the mantle and avoiding an excess of silicon and oxygen in the core. The transport of magnesium as oxide or silicate from the cooling core to underneath the mantle is an order of magnitude more efficient per unit mass as a source of buoyancy than inner-core growth. We therefore conclude that Earth's dynamo would survive throughout geologic time (from at least 3.4 billion years ago to the present) even if core radiogenic heating were minimal and core cooling were slow.