IMPORTANCE: Proton pump inhibitors (PPIs) and histamine 2 receptor antagonists (H2RAs) suppress the production of gastric acid and thus may lead to malabsorption of vitamin B12. However, few data exist regarding the associations between long-term exposure to these medications and vitamin B12 deficiency in large population-based studies. OBJECTIVE: To study the association between use of PPIs and H2RAs and vitamin B12 deficiency in a community-based setting in the United States. DESIGN, SETTING, AND PATIENTS: We evaluated the association between vitamin B12 deficiency and prior use of acid-suppressing medication using a case-control study within the Kaiser Permanente Northern California population. We compared 25,956 patients having incident diagnoses of vitamin B12 deficiency between January 1997 and June 2011 with 184,199 patients without B12 deficiency. Exposures and outcomes were ascertained via electronic pharmacy, laboratory, and diagnostic databases. MAIN OUTCOMES AND MEASURES: Risk of vitamin B12 deficiency was estimated using odds ratios (ORs) from conditional logistic regression. RESULTS: Among patients with incident diagnoses of vitamin B12 deficiency, 3120 (12.0%) were dispensed a 2 or more years’ supply of PPIs, 1087 (4.2%) were dispensed a 2 or more years’ supply of H2RAs (without any PPI use), and 21,749 (83.8%) had not received prescriptions for either PPIs or H2RAs. Among patients without vitamin B12 deficiency, 13,210 (7.2%) were dispensed a 2 or more years’ supply of PPIs, 5897 (3.2%) were dispensed a 2 or more years’ supply of H2RAs (without any PPI use), and 165,092 (89.6%) had not received prescriptions for either PPIs or H2RAs. Both a 2 or more years’ supply of PPIs (OR, 1.65 [95% CI, 1.58-1.73]) and a 2 or more years’ supply of H2RAs (OR, 1.25 [95% CI, 1.17-1.34]) were associated with an increased risk for vitamin B12 deficiency. Doses more than 1.5 PPI pills/d were more strongly associated with vitamin B12 deficiency (OR, 1.95 [95% CI, 1.77-2.15]) than were doses less than 0.75 pills/d (OR, 1.63 [95% CI, 1.48-1.78]; P = .007 for interaction). CONCLUSIONS AND RELEVANCE: Previous and current gastric acid inhibitor use was significantly associated with the presence of vitamin B12 deficiency. These findings should be considered when balancing the risks and benefits of using these medications.