Psalm 34 takes place after David went to the Philistine city of Gath but found no refuge there and narrowly escaped. He, then, went to Adullam Cave where many desperate men joined him. This joyful and wise Psalm seems to have been written from that cave, and sung in the presence of those men.
Here is a link to David Guzik's commentary on Psalm 34: https://enduringword.com/biblecommen...