In Psalms 23, David is praising God. David compares God to a shepherd. In the Israelite culture, a shepherd was more like a marine. Shepherds were brawlers who honorably watched their flock against wild animals. That is the image David is using for God:
Psa 23:1 A Psalm of David. The LORD is my shepherd; I shall not want.
Psa 23:2 He makes me to lie down in green pastures; He leads me beside the still waters.
Psa 23:3 He restores my soul; He leads me in the paths of righteousness For His name’s sake.
In the first three verses, David emphasizes God compassion. A good shepherd ensures the flock is well feed and happy. David compares a shepherd leading a flock to God leading the way for righteousness. The imagery is that God is leading (not pushing from behind) and David is willingly following. The end path is righteousness.
Psa 23:4 Yea, though I walk through the valley of the shadow of death, I will fear no evil; For You are with me; Your rod and Your staff, they comfort me.
The next image is a juxtaposition against the green pastures. Multiple times in David’s life he was almost killed. Saul attempted to kill David throughout 1 Samuel and David may have only survived through divine intervention. A good shepherd protects his flock. David parallels the strength of a shepherd with the strength of God. David takes solstice in God’s weapons (the shepherd’s rod and staff). David knows that even though he might be in a dangerous position, God will deliver him (as God had done consistently in the past).
Psa 23:5 You prepare a table before me in the presence of my enemies; You anoint my head with oil; My cup runs over.
David speaks again of his encounters with his enemies. David’s army on several occasions were in close proximity to that of Saul’s. Even when David’s enemies were at arms length away (1 Sam 24:4), God supplied David with resources. David points out that this is because David is God’s anointed. The image is that God is so liberal in His blessings that much of the blessings spill over and cannot be possibly used. David is truly blessed.
Psa 23:6 Surely goodness and mercy shall follow me All the days of my life; And I will dwell in the house of the LORD Forever.
David’s character was very eccentric. David would sometimes dance before God and sometimes fight giants with only a sling (trusting in only God for the victory). David was confident that God would bless him throughout his entire reign. David dies of old age in 1 Kings 2.