Context. The study of abundances in the nucleus of active galaxies allows us to investigate the evolution of the abundance by comparing local and higher redshift galaxies. However, the methods used so far have substantial drawbacks or rather large uncertainties. Some of the measurements are at odds with the initial mass function derived from the older stellar population of local elliptical galaxies. Aims. We determine accurate and reliable abundances of C, N, Ne, and Fe relative to O from the narrow absorption lines observed in the X-ray spectra of Mrk 509. Methods. We use the stacked 600 ks XMM-Newton RGS and 180 ks Chandra LETGS spectra. Thanks to simultaneous observations with INTEGRAL and the optical monitor on-board XMM-Newton for the RGS observations and HST-COS and Swift for the LETGS observations, we have an individual spectral energy distribution for each dataset. Owing to the excellent quality of the RGS spectrum, the ionisation structure of the absorbing gas is well constrained, allowing for a reliable abundance determination using ions over the whole observed range of ionisation parameters. Results. We find that the relative abundances are consistent with the proto-solar abundance ratios: C/O = 1.19 ± 0.08, N/O = 0.98 ± 0.08, Ne/O = 1.11 ± 0.10, Mg/O = 0.68 ± 0.16, Si/O = 1.3 ± 0.6, Ca/O = 0.89 ± 0.25, and Fe/O = 0.85 ± 0.06, with the exception of S, which is slightly under-abundant, S/O = 0.57 ± 0.14. Our results, and their implications, are discussed and compared to the results obtained using other techniques to derive abundances in galaxies.