The psalms are songs about real life. Like some other psalms of David, he also wrote this one when he had to flee from Saul (Psalm 56:1–13, Psalm 57:1–11, Psalm 142:1–7). It is important to know this background to understand the Psalm better. It is also helpful to study 1 Samuel 21:1–15 (especially 1 Samuel 21:10–15). But on the other hand that unique situation in Gath is not explicitly mentioned in the Psalm. The verses in the Psalm are written in a general style, so that the poem can be used by different people in different situations. David wants to teach everyone to praise the Lord for his saving grace (Psalm 34:1–10) and to live for him in respectful fear (Psalm 34:11–22).
1 I will bless the LORD at all times; his praise shall continually be in my mouth.