Immature mucosal defenses contribute to increased susceptibility of newborn infants to pathogens. Sparse knowledge of age-dependent changes in mucosal immunity has hampered improvements in neonatal morbidity because of infections. We report that exposure of neonatal mice to commensal bacteria immediately after birth is required for a robust host defense against bacterial pneumonia, the leading cause of death in newborn infants. This crucial window was characterized by an abrupt influx of interleukin-22 (IL-22)–producing group 3 innate lymphoid cells (IL-22+ILC3) into the lungs of newborn mice. This influx was dependent on sensing of commensal bacteria by intestinal mucosal dendritic cells. Disruption of postnatal commensal colonization or selective depletion of dendritic cells interrupted the migratory program of lung IL-22+ILC3 and made the newborn mice more susceptible to pneumonia, which was reversed by transfer of commensal bacteria after birth. Thus, the resistance of newborn mice to pneumonia relied on commensal bacteria–directed ILC3 influx into the lungs, which mediated IL-22–dependent host resistance to pneumonia during this developmental window. These data establish that postnatal colonization by intestinal commensal bacteria is pivotal in the development of the lung defenses of newborns.