Optimization of water pumping systems has been studied using various techniques which include classical, mathematical, and heuristics. Few studies have explored use of optimal controllers in agricultural water pumping applications. Some studies also ignore the interconnection between the water demand and energy used. Introduction of renewable energy sources such as photovoltaic (PV) necessitates different geographical studies as the intensity of the renewable energy varies widely with location. In this paper, an optimal controller for a batteryless grid-connected photovoltaic system to power water supply system for irrigation purposes was developed. The aim was to minimize the operational cost of grid energy by maximizing utilization of photovoltaic power and minimizing the utilization of grid power. A case study was done at a farm in Kajiado (−1.6033257^{circ } latitude and 36.7863352^{circ } longitude). The farm photovoltaic, grid power, water pumps (underground and booster pump), and storage tanks were modelled into a binary linear programming optimization problem and solved using intlinprog solver on MATLAB. Energy demand data was collected using a three-phase power logger, while water demand data was collected using onsite water meter and stopwatch timer. Photovoltaic power produced was estimated using Photovoltaic Geographical Information System (PVGIS). Simulation results obtained show that the use of an optimal controller results in reduced cost of energy by 44.4%. Better utilization of renewable photovoltaic energy by 24% was observed, and 3.6% more water was pumped.