There is a custom to recite psalm 27, L’David Hashem ori v’yish’i (see here for full Hebrew and English text of this psalm), from the beginning of the month of Elul (there is difference of opinion as to when this psalm should be said until) after prayer services. The source for this custom is relatively unclear and whether one should follow this custom is also under dispute. Dr. Shnayer Leiman (bio here and here) traces the origins of this custom and provides some interesting insights. The audio lecture can be found here. The following is a summary Dr. Leiman's lecture.
This custom is not mentioned in Shas or Shulchan Aruch, however the Mishna Brura סימן תקפא ס'ק ב' tells us that it is “our” custom to recite psalm 27 from the beginning of Elul until Yom Kippur. This raises a few questions, namely, what is the connection of this psalm to this period of time, why should one say it and what is the source of this custom?
The Mateh Ephraim (19th century, which predates the Mishna Brura) instructs one to follow this custom and the Elef L’mateh, commenting on the Mateh Ephraim, provides an explanation for the relationship of this psalm to this period of time which is based upon the Midrash Shocher Tov, a Midrash on Tehilim. The explanation provided is that ori refers to Rosh Hashana and yish’i refers to Yom Kippur, therefore it is appropriate to recite it during this period. There is also a further allusion in the psalm to Succos and therefore the Mateh Ephraim adds that it is his custom to recite this psalm until Shemini Atzeres (however the allusion to Succos is not found in the midrash). What is interesting to note regarding the Midrash is that the interpretation of the words referring to Rosh Hashana and Yom Kippur follows many other alternative interpretations which have no connection to Rosh Hashana and Yom Kippur. Furthermore, the Midrash does not instruct one to recite this psalm during this period of time. Therefore one must wonder whether this source is compelling enough for this custom to arise.
There are also several opinions which state that this custom should not be adhered to.
According to the customs of the Vilna Gaon found in Maaseh Rav אות נג, one should not say psalm 27 from Rosh Chodesh Elul until Yom Kippur (there is also no mention of Succos according to the Vilna Gaon). The Nitei Gavriel states that certain gedolim of various Chasidic dynasties also do not say psalm 27, such as Apter Chasidim, Zidichov, Kalev and Tsanz. The Otzer Chaim (minhagim of Tanz) says not to say it since it does not mention to do this in the Kavanos of the Arizal (This is also quite curious as there are many customs which are followed and are not mentioned in the Arizal).
It is claimed that the first time that one should say psalm 27 is found in Chemdas Yamim, This book of anonymous authorship is quite controversial since some have cautioned against its Sabbatean influences. Some of the hints to this influence may be due to the fact that in the Amsterdam print there is a picture of Natan Azati, the “prophet” of Shabetai Zvi, in the beginning of the book, as well as a poem which has within it an acrostic spelling out Ani Binyamin Natan ben Elisha Chaim (the name of Natan Azati). There it claims that it was the custom to say psalm 27 during the month of Elul.
Regarding the reasons as to why one should say this psalm, specifically during this period of time, the Shem Tov Katan (a kabbalistic work authored by R. Binyamin Beinish and published in 1706) explains that all those who say psalm 27 from Rosh Chodesh Elul until after Simchas Torah will have all their negative spiritual decrees against them nullified. Additionally, the Sefer Zechira (published in Hamburg in 1709, before the publication of Chemdas yamim) claims that all those who say this psalm morning and night will have all their days filled with good and the heavenly prosecutors will have no dominion over them! The nullification of negative spiritual decrees would be most appealing since the period of Elul until Yom Kippur (or Simchas Torah) is one which is directly connected to Divine judgment.
The final piece of the puzzle is to be found in the Nezer HaKodesh (minhagim of Ropshitz).There it says to say psalm 27 after prayer services. A story is related by Moshe David Strum (?) (Talna) who says that R. Avraham Shimon of Zelochov (Mashgiach of Yeshivas Chachmei Lublin) once came to the beis midrash of R. Aryeh Leibish of Tsanz (grandson of R. Chaim Tzanzer) and asked the Avreichim why the Rebbe does not say psalm 27, whereas in Shinover (R. Chaim Tzanzer’s son) they do say it? The answer given explains the reason for the dispute amongst certain sects of Chasidim reciting this psalm as well as revealing the source for this custom. The story goes as follows. In the time of R. Eliyahu Baal Shem of Chelm (16th century, contemporary of the Maharshal) the ruler of Poland did not have children and consequently ordered the Jews to pray that he should have children. The repercussions for the prayers not being answered would be severe; all the Jews from Poland would be expelled. R. Eliyahu Baal Shem of Chelm told the ruler that he would have a son within 12 months! When the Baal Shem Tov (Rabbi Yisrael Baal Shem) used to tell over this story to his students he would tell them not to assume that this was an easy proclamation for R. Eliyahu Baal Shem. Initially R. Eliyahu Baal Shem of Chelm went to the “good” forces (angels?) to plead for a son for the ruler of Poland, they said no. Thereafter he went to the “evil” forces and they also said no, until he went the sitra achra himself to plead! The result of bargaining with these impure forces was that the two prayers that R. Eliyahu Baal Shem of Chelm instituted within the liturgy were going to be nullified. The two prayers which he instituted were the recitation of psalm 27 during the month of Elul (our topic at hand) and k’gavna (a portion of the Zohar recited after kabbalas Shabbos, immediately preceding Maariv). R. Avraham Shimon of Zelochov (the Rabbi who presented the question in the beginning of the story) said that the Apter Rav did not say 27 and R Elimelech of Lizhensk did not say K’gavna. This is why, explained R. Avraham Shimon of Zelochov, there is a difference of opinion regarding the recitation of psalm 27 amongst these Chasidic dynasties. Each student took on one custom and not the other (resulting in a fulfillment of the decree that the two instituted prayers by R. Eliyahu Baal Shem of Chelm were to be nullified).
From this interesting and elaborate story we see that the origin of the recital of psalm 27 during Elul is from R. Eliyahu Baal Shem of Chelm in the 16th century. The reason why certain Chasidim recite this psalm whereas others do not, is due to the custom that was accepted by his students in order to fulfill the decree that his two instituted prayers would be nullified. The reasons for the relevance of this psalm during Elul are those given by the Shem Tov Katan and the Sefer Zechira, that it has the power to wipe out the negative influences of the heavenly prosecutors! However, as mentioned before, the Vilna Gaon did not subscribe to this custom and ordered against it.
The questions that remain are why do Jews who are not of Chasidic descent recite this psalm? Furthermore, and perhaps most importantly, what was the source upon which R. Eliyahu Baal Shem of Chelm saw fit to institute the recitation of the psalm as late as the 16th century, without any mention of the custom in the earlier works of our sages?