The serotonin 4 receptor (5-HT4R) is a promising target for the treatment of depression. Highly selective 5-HT4R agonists, such as prucalopride, have antidepressant-like and procognitive effects in preclinical models, but their clinical effects are not yet established. To determine whether prucalopride (a 5-HT4R agonist and licensed treatment for constipation) is associated with reduced incidence of depression in individuals with no past history of mental illness, compared with anti-constipation agents with no effect on the central nervous system. Using anonymised routinely collected data from a large-scale USA electronic health records network, we conducted an emulated target trial comparing depression incidence over 1 year in individuals without prior diagnoses of major mental illness, who initiated treatment with prucalopride versus two alternative anti-constipation agents that act by different mechanisms (linaclotide and lubiprostone). Cohorts were matched for 121 covariates capturing sociodemographic factors, and historical and/or concurrent comorbidities and medications. The primary outcome was a first diagnosis of major depressive disorder (ICD-10 code F32) within 1 year of the index date. Robustness of the results to changes in model and population specification was tested. Secondary outcomes included a first diagnosis of six other neuropsychiatric disorders. Treatment with prucalopride was associated with significantly lower incidence of depression in the following year compared with linaclotide (hazard ratio 0.87, 95% CI 0.76-0.99; P = 0.038; n = 8572 in each matched cohort) and lubiprostone (hazard ratio 0.79, 95% CI 0.69-0.91; P < 0.001; n = 8281). Significantly lower risks of all mood disorders and psychosis were also observed. Results were similar across robustness analyses. These findings support preclinical data and suggest a role for 5-HT4R agonists as novel agents in the prevention of major depression. These findings should stimulate randomised controlled trials to confirm if these agents can serve as a novel class of antidepressant within a clinical setting.