In 1 Samuel 17:4, 57-58 David is the giant killer and writes Psalm 8. We believe this because in the most ancient Jewish Targums—paraphrases of the Hebrew Old Testament into Aramaic from the time of Ezra onward—specifically point to this 8th Psalm as being about David and Goliath.
The words in the manuscripts before Psalm 9 are actually the ending of Psalm 8. Muthlabben means ‘death of champion’ and was paraphrased in the Targums referring to David’s killing the ‘man of the space between the camps’ in 1 Samuel 17:4. That no mans land was dominated by Goliath and was conquered by David.
Much like Satan was defeated by Christ’s coming to earth. David may have sung this Psalm while in Saul’s court to comfort him when the demons troubled him.