In 1 Samuel 21 & 22, we read that when Jonathan confirmed to David that Saul was trying to kill him. David fled and went to Nob where he met with the priest, Ahimelech. David couldn’t tell Ahimelech why he was there so he made up a story to protect Ahimelech. Then, David asked for food and a weapon and received bread and Goliath’s sword.
Observing all of this was a man named Doeg; he was an Edomite, a Descendant of Esau. Doeg is described as “in charge of Saul’s servants” which probably means he was a general in the army. Saul was complaining to “his servants” about not being informed about Jonathan helping David escape. Doeg recalled to Saul about seeing David and Ahimelech together and David receiving the bread and a sword, but he added to the story by adding that Ahimelech “inquired of the Lord for him”. Upon hearing that, Saul sent for Ahimelech and all the priests in his family. Ahimelech was confronted by Saul and proclaimed his innosence, but Saul had him and all the priests killed except one who escaped and fled to David. This is the background for Psalm 52.
David blamed himself for bringing this on Ahimelech and his family. Many people were grieving in the town of Nob; evidently, Ahimelech was high priest for the nation at that time. This psalm may have been directed at Doeg or Saul or both, but David realized he did not have to take revenge; God would settle the score (v. 5-7). In verses 8-9, David praises God because God is the One who judges and punishes with perfect justice.