Sour gas reservoirs represent significant sources of energy with substantial development potential, playing a vital role in enhancing energy security and diversity. However, the production and processing of sour gas pose serious challenges as a result of its corrosive nature. The literature reveals a lack of efficient green inhibitors for sour media, because traditional inhibitors often raise environmental concerns. Here, sunflower oil was employed to develop novel quaternary ammonium salts based on pyrrolidine (PyQAs) and piperidine (PiQAs), serving as highly biodegradable and effective sour corrosion inhibitors. The synthesis process involved a straightforward two-step reaction under mild and solventfree conditions. Experimental results demonstrated a remarkable 95% reduction in the corrosion rate in the presence of PyQAs and PiQAs. PiQAs exhibited higher performance, achieving a remarkable 95.2% inhibition efficiency at a concentration of 70 × 10 −5 M. Quantum calculations indicated that PiQAs, with its narrower band gap and enhanced electron transfer ability, emerged as a more effective inhibitor. Moreover, the molecular structures of PyQAs and PiQAs, particularly the electron-donating alkyl chains and the ester groups, facilitated their adsorption onto the steel surface. The inhibition mechanism was attributed to the formation of a dense film on the mild steel surface, which hindered the transport of corrosive ions and decreased the electrochemical reactions associated with corrosion. Atomic force microscopy images showed that the addition of PyQAs and PiQAs significantly reduced the surface roughness. Furthermore, both PyQAs and PiQAs were highly biodegradable, with biodegradation degrees of 57.8 and 53.1%, respectively, highlighting their environmental compatibility. These compounds offer a promising balance of corrosion inhibition performance and biodegradability, making them attractive alternatives to traditional inhibitors for industrial applications.