The title of the psalm is: A song at the dedication of the temple.
The Bible also mentions that David is the author. But that is a problem: there was no temple in the time of David. His son Solomon built the temple. The Hebrew title of this psalm is: A song at the dedication of the house.
That house
can be (1) the house of David or (2) the house of the Lord. We believe it was specifically written for the dedication of the house of David, but later also used for the dedication of the temple.
1 I will extol you, O LORD, for you have drawn me up and have not let my foes rejoice over me.