Q. Who wrote Psalm 23?
A. Like all but 24 of the 150 biblical psalms, Ps 23 begins with a title or an introductory note. In fact, it begins with the most common introductory note in the Psalter consisting of a single Hebrew word: le-david. This word, which prefaces 73 of the psalms, is usually translated into English as “of David” and has traditionally been interpreted as indicating King David’s authorship of these particular psalms.
However, the prepositional prefix “le” is very ambiguous and could legitimately be translated in many different ways (of, dedicated to, in the tradition of). Also David clearly could not have written some of the psalms prefaced by le-david because they contain references to later (post-Davidic) events, such as Ps 69, which refers to the Babylonian exile (c. 586-539 B.C.E.). So the presence of the title le-david doesn’t really answer your question.
In favor of Davidic authorship is a biblical and post-biblical tradition connecting David to music and poetry (1Sam 16:17-23; 2Sam 1:17-27; Amos 6:5 1Chr 16:7-36). If this tradition has some basis in historical fact (as most traditions do), we can safely assume that David wrote some songs and inspired others. But are any of these songs preserved in the Psalter and is Ps 23 one of them? We don’t know for sure.
The Psalter is an anthology compiled over a long period of time (perhaps centuries) and finalized in the 5th or 4th century B.C.E. In its final form, it is divided into 5 parts, imitating the structure of the Torah, with many Davidic psalms in Part I (Ps 1-41). More Davidic psalms appear later in the book, even after the statement at the end of Ps 73 that “The prayers of David son of Jesse are ended.” Other psalms are associated with temple singers such as Asaph (Ps 50, Ps 73-83) and Heman (Ps 88) or no one at all.
David clearly becomes identified with musical worship in Israel’s literary tradition. Later psalms were likely attributed to him for this reason, just as post-exilic wisdom texts were attributed to Solomon and Israel’s constantly expanding legal tradition continued to be attributed to Moses.