Microbial community assembly mechanisms are pivotal for understanding the ecological functions of microorganisms in biogeochemical cycling in Earth's ecosystems, yet rarely investigated in the context of deep terrestrial ecology. Here, the microbial communities in the production waters collected from water injection wells and oil production wells across eight oil reservoirs throughout northern China were determined and analyzed by proportional distribution analysis and null model analysis. A ‘core’ microbiota consisting of three bacterial genera, including Arcobacter, Pseudomonas and Acinetobacter, and eight archaeal genera, including Archaeoglobus, Methanobacterium, Methanothermobacter, unclassified Methanobacteriaceae, Methanomethylovorans, Methanoculleus, Methanosaeta and Methanolinea, was found to be present in all production water samples. Canonical correlation analysis reflected that the core archaea were significantly influenced by temperature and reservoir depth, while the core bacteria were affected by the combined impact of the core archaea and environmental factors. Thermodynamic calculations indicate that bioenergetic constraints are the driving force that governs the enrichment of two core archaeal guilds, aceticlastic methanogens versus hydrogenotrophic methanogens, in low- and high-temperature oil reservoirs, respectively. Collectively, our study indicates that microbial community structures in wells of oil reservoirs are structured by the thermodynamic window of opportunity, through which the core archaeal communities are accommodated directly followed by the deterministic recruiting of core bacterial genera, and then the stochastic selection of some other microbial members from local environments. Our study enhances the understanding of the microbial assembly mechanism in deep terrestrial habitats. Meanwhile, our findings will support the development of functional microbiota used for bioremediation and bioaugmentation in microbial enhanced oil recovery.