The psalm was used on very special occasions. It was used at the dedication of the temple after it was defiled by the heathens. That happened twice. The first time was after the destruction of God’s house by the Babylonians. After the exile, God’s people returned to the Promised Land and rebuilt the temple. (See Ezra 6:13–18 where is written that the rebuilding of the temple was finished and dedicated.) And the second time was after the desecration of the temple by a Syrian king. That was still remembered in the time of Jesus at the Feast of Dedication (John 10:22). This festival, now called Hanukkah, commemorates the restoration of the temple in the time of Judas Maccabeus and the Jewish revolt against Antiochus IV Epiphanes (164 BC).
1 I will extol you, O LORD, for you have drawn me up and have not let my foes rejoice over me.