This paper describes the optimization and validation of an analytical method for the determination of 83 pharmaceutically active compounds (PhACs) in aqueous samples using solid-phase extraction (SPE) followed by ultra performance liquid chromatography-triple quadrupole mass spectrometry (UPLC-QqQ-MS/MS). First, several experiments were conducted to optimize different SPE extraction parameters such as pH, elution solvents, and Na2EDTA addition. Extraction recovery percentages were between 17 and 146%, being higher than 70% for 47 target analytes. The effect of salinity in the extraction efficiency proved to be negligible (< 20% within a range from 0 to 38 g L-1). The method limits of detection (LOD) and quantification (LOQ) were below 1 ng L-1 for most compounds (> 90%), and the precision of the method, calculated as the relative standard deviation (RSD) of replicate extractions and analyses, was less than 20%. The optimized method was successfully applied to the analysis of real water samples in estuarine and coastal systems from SW Spain (Cadiz Bay and Huelva Estuary). 49 out of 83 target compounds were found in 75% of samples. Ibuprofen, atenolol, gemfibrozil and caffeine were the most commonly detected substances, reaching concentrations up to 195 ng L-1.