Psalm 30 offers an interesting curiosity in its very first line – “A song used at the dedication of the temple.” The meaning is unclear, because Solomon did not build the Temple until long after David’s death. It is possible that David wrote this for that purpose before he died or that he had written it previously on another occasion, and it was chosen for the Temple dedication. There is nothing in the psalm itself that points to the Temple or even the Tabernacle, where David would have worshiped, and neither 1 Kings 8 nor 2 Chronicles 7 include this psalm in the dedication accounts.
David began by praising God for three things he had done for David: 1) he exalted David over his enemies; 2) he healed David; and 3) he pulled David back from the brink of death (Psalm 30:1-3). Because of this, David encouraged God’s people to praise along with him (Psalm 30:4-5). They were to celebrate the truth that, no matter how dark our immediate situation, “joy arrives in the morning.”
Verses 6-12 seem to further explain verse three. In a line reminiscent of Psalm 27:1, David felt secure in his relationship with God, but something changed. Feeling rejected by God, David cried out with a lament, begging for mercy. Psalm 30:9-10 records that prayer, while Psalm 30:11-12 shows the result. God answered David’s prayer, rescuing him from death. This changed David’s attitude from “lament into dancing,” sparking the praise that led him to write this psalm.