Psalm 138 was likely said while offering a thanksgiving sacrifice in the temple. The speaker recounts how God answered his/her prayer, praises God, and expresses confidence that God will continue to grant protection in the future.
Psalm 138 is a direct-address to God which expresses thanks for his salvation.
1b-2b. Offeratory formulas
3. Account of prayer and salvation
4-5. Anticipated thanks/offerings of kings
6. Hymnic praise
7-8b. Affirmation of confidence
Psalm 138 contains many elements of the Thanksgiving genre (see Gerstenberger’s “Introduction to Cultic Poetry” in vol. 1 of “Psalms”): (a) it has the offertory formula ‘odekha “I will praise you” (vv. 1-2), (b) it gives an account of trouble and salvation (v. 3), and (c) it expresses praise for God (v. 6). Yet, Psalm 138 is different than all other thanksgiving psalms in that it contains no public aspects: there isn’t (a) an invitation to give thanks or to praise, (b) a blessing for the participants, or (c) an exhortation towards the community. What is one to make of this break from convention?
[For an example of “public service” in individual thanksgivings, see Ps. 22:23-27: Then will I proclaim Your fame to my brethren, praise You in the congregation. You who fear the LORD, praise Him! All you offspring of Jacob, honor Him! Be in dread of Him, all you offspring of Israel! For He did not scorn, He did not spurn the plea of the lowly; He did not hide His face from him; when he cried out to Him, He listened. Because of You I offer praise in the great congregation; I pay my vows in the presence of His worshipers. Let the lowly eat and be satisfied; let all who seek the LORD praise Him. Always be of good cheer!]
Some scholars point out that while most thanksgiving psalms do not contain petitions, Psalm 138 does: “do not forsake the work of Your hands” (v. 8). Based on this peculiarity they suggest that the psalm was once a complaint/petition, and it was later changed to conform to the thanksgiving genre. But, this is highly speculative because (a) the petition is short and run-of-the-mill, and (b) some other thanksgivings also contain petitions, e.g. Ps. 40:12 and 118:25. Others suggest that Psalm 138 is a royal psalm (based on the mention of kings in v. 4 and parallels to Ps. 18), but many are just not sure.
Although I have not seen it written anywhere, I think that there is an altogether different reason why Psalm 138 lacks any public characteristics: it was said by an unaccompanied individual. The psalm mentions the “lowly” and those “from afar” in v. 6, and it is possible that the reciter came to the temple alone (which would make sense if he came from afar). Thus, it is a normal thanksgiving psalm, just said in an unordinary circumstance.
The psalm’s setting seems to be the temple: “I bow toward Your holy temple and praise Your name” (v. 2). It begins with an emphatic line, “I praise You with all my heart,” but quickly becomes enigmatic: what does neged ‘elohim ‘azammerekha “I will sing a hymn to You before the divine beings” mean? While some choose to translate this phrase as “in front, God, I will sing to you,” this is a difficult reading. Rather, like Ps. 82:1 (“God stands in the divine assembly; among the divine beings He pronounces judgment”) and Ps. 135:5 (“For I know that the LORD is great, that our LORD is greater than all gods”) our verse seems to acknowledge the existence – albeit a diminished one – of other gods. This is what scholars call monolatry, i.e. the worship of one deity while acknowledging the existence of others. Monotheism, they say, only developed later.
The psalm mentions the individual “offertory formula” ‘odekha twice (vv. 1-2), and this was probably said while bringing the sacrifice. The account of salvation (v. 3) is brief: “When I called, You answered me, You inspired me with courage.” It is also unusual because it is a direct-address as opposed to the normal third person description (cf. 118:17-20, 40:2-4, 116:3-4). But, it isn’t altogether peculiar: Ps. 30:3-5 and 116:8 are also direct-address accounts of salvation.
Vv. 4-5 express hope for future praise: “All the kings of the earth shall praise You, O LORD, for they have heard the words You spoke. They shall sing of the ways of the LORD, ‘Great is the majesty of the LORD!’” V. 6 is a single line of hymnic praise, and vv. 7-8 are an affirmation of confidence (usual for complaint/petitions but unusual for thanksgivings, but still see Ps. 18:29-30, 118:6-7,14). It ends with a petition, also unusual for thanksgivings, but similar to Ps. 40:12 and 118:25.
V. Important verses
v. 1: Of David. I praise You with all my heart, sing a hymn to You before the divine beings
v. 3: When I called, You answered me, You inspired me with courage.
v. 6: High though the LORD is, He sees the lowly; lofty, He perceives from afar.