The measurements of the production of prompt D0 , D+ , D∗+ , and D+s mesons in proton–proton (pp) collisions at s√=5.02 TeV with the ALICE detector at the Large Hadron Collider (LHC) are reported. D mesons were reconstructed at mid-rapidity ( |y|<0.5 ) via their hadronic decay channels D0→K−π+ , D+→K−π+π+ , D∗+→D0π+→K−π+π+ , D+s→ϕπ+→K+K−π+ , and their charge conjugates. The production cross sections were measured in the transverse momentum interval 0<pT<36 GeV/c for D0 , 1<pT<36 GeV/c for D+ and D∗+ , and in 2<pT<24 GeV/c for D+s mesons. Thanks to the higher integrated luminosity, an analysis in finer pT bins with respect to the previous measurements at s√=7 TeV was performed, allowing for a more detailed description of the cross-section pT shape. The measured pT -differential production cross sections are compared to the results at s√=7 TeV and to four different perturbative QCD calculations. Its rapidity dependence is also tested combining the ALICE and LHCb measurements in pp collisions at s√=5.02 TeV . This measurement will allow for a more accurate determination of the nuclear modification factor in p–Pb and Pb–Pb collisions performed at the same nucleon–nucleon centre-of-mass energy.