Photocatalytic conversion of CO2 to CH3CHO is of increasing interest but confronts the significant challenges of forming C–C bonds and keeping the C[double bond, length as m-dash]O bond intact throughout the process. Here, we report the selective photocatalytic hydrogenation of CO2 to CH3CHO using a modified polymeric carbon nitride (PCN) under mild conditions. The locally crystallized PCN offers a photocatalytic activity of 1814.7 μmol h−1 g−1 with a high selectivity of 98.3% for CH3CHO production and a quantum efficiency of 22.4% at 385 nm, outperforming all the state-of-art CO2 photocatalysts. The promoted formation of the *OCCHO intermediate on the locally crystallized PCN is disclosed as the key factor leading to the highly selective CH3CHO generation. The locally crystallized PCN favors spontaneous C–C coupling towards *OCCHO formation rather than *CHO protonation, thus preventing HCHO formation. This work provides a new strategy for designing carbon nitrides for highly selective and sustainable conversion of CO2 to CH3CHO.