Oral anti-diabetic drugs (OADs) have been associated with community-acquired pneumonia (CAP). We aimed to validate the recording of CAP in the Spanish Database for Pharmacoepidemiological Research in Primary Care (BIFAP) for the future evaluation of OAD–CAP association. The incidence rate (IR/1000 person-years) of CAP in type 2 diabetes mellitus (T2DM) was also determined. In total, 2966 pneumonia records (2040 listed as diagnosis and 926 as identified from comments added by physicians) were identified from 76 009 patients with T2DM after the first OAD in 2002–2013. Data around the CAP date were reviewed: 1803 (60·9%) were classified as ‘probable CAP’ (confirmed by X-ray/laboratory, referral letters or CAP lung site); 589 (19·8%) as ‘no-case’ (486 had other illness, 78 previous CAP, 25 cancer); and 574 (19·4%) as ‘possible CAP’ (441 without confirmatory information, 133 with uncertain diagnosis or uncertain diagnosis date). In total, 74·2% and 31·4% of pneumonia records in the diagnosis and comments, respectively, were ‘probable cases’ (IR: 6·04), which increased to 90·5% and 42·9%, respectively, when the 441 ‘possible cases’ without confirmatory information were included (IR: 7·52). In summary, diagnosis had a high positive predictive value, and adding cases automatically detected from comments decreased that value significantly.