Psalm 23 was written by David when his forces were fighting with the forces of his son Absalom, who had rebelled against him. Absalom wanted to become the King of Israel, and this forced David to run away Gilead. In Gilead, he met Barzillai, who was a shepherd.
David used an idiomatic expression in Psalm 23 to show the relationship he had with God and who He was to him. In this passage of scripture, God is the shepherd, green fields are the abundance of food and the still water is joy and rest. Psalms 23 is one of the well-known portions of the scripture.