Opioid prescribing for postoperative pain management is challenging because of inter‐patient variability in opioid response and concern about opioid addiction. Tramadol, hydrocodone, and codeine depend on the cytochrome P450 2D6 (CYP2D6) enzyme for formation of highly potent metabolites. Individuals with reduced or absent CYP2D6 activity (i.e., intermediate metabolizers [IMs] or poor metabolizers [PMs], respectively) have lower concentrations of potent opioid metabolites and potentially inadequate pain control. The primary objective of this prospective, multicenter, randomized pragmatic trial is to determine the effect of postoperative CYP2D6‐guided opioid prescribing on pain control and opioid usage. Up to 2020 participants, age ≥8 years, scheduled to undergo a surgical procedure will be enrolled and randomized to immediate pharmacogenetic testing with clinical decision support (CDS) for CYP2D6 phenotype‐guided postoperative pain management (intervention arm) or delayed testing without CDS (control arm). CDS is provided through medical record alerts and/or a pharmacist consult note. For IMs and PM in the intervention arm, CDS includes recommendations to avoid hydrocodone, tramadol, and codeine. Patient‐reported pain‐related outcomes are collected 10 days and 1, 3, and 6 months after surgery. The primary outcome, a composite of pain intensity and opioid usage at 10 days postsurgery, will be compared in the subgroup of IMs and PMs in the intervention (n = 152) versus the control (n = 152) arm. Secondary end points include prescription pain medication misuse scores and opioid persistence at 6 months. This trial will provide data on the clinical utility of CYP2D6 phenotype‐guided opioid selection for improving postoperative pain control and reducing opioid‐related risks.