David wrote Psalm 23 to express his deep faith and reliance on God as a shepherd who provides comfort, guidance, and protection. This psalm reflects David's personal experiences, illustrating his trust in God during difficult times. It serves as a source of hope and reassurance for believers seeking God's presence in their lives.