English Basic Explanation - Chapter 30
In this Psalm, which we recite each day towards the beginning of the morning Pesukei De'zimra service, David jubilantly celebrates his having been rescued from dire straits. He speaks of God saving him from "She'ol" (the underworld – verse 4) and transforming his mourning into exuberant joy (verse 12). David's description of a person weeping at night and then celebrating in the morning (verse 6) suggests that he has just experienced a sudden salvation, finding himself at the brink of death one moment and then, at the next, in a state of sheer euphoria.
Rashi and Radak explain that David refers here not to any kind of physical salvation, but rather to the forgiveness he was granted for his sin with Batsheva. His adversaries relentlessly criticized David and undermined his stature due to this incident, claiming that his dynasty cannot possibly endure, certainly not through Shelomo, a son born to him from Batsheva herself. According to Rashi, David was even told by his antagonists that he forfeited his share in the next world as a result of this sin. David himself, it appears, suffered considerable anxiety and distress worrying about the future of his dynasty and his soul, until, as the Radak explains, the prophet Natan conveyed God's message to David assuring him that his son will assume the throne and construct the Midkash (see Shemuel II, chapter 7). It was at that point, the Radak claims, that David composed this Psalm, to express his gratitude to the Almighty for granting him forgiveness and for guaranteeing him the continuation of his kingship after his death.
As the first verse of this chapter states, David composed this Psalm for the occasion of the "Chanukat Ha'bayit," the dedication of the Temple. The successful completion and dedication of the Beit Ha'mikdash built by Shelomo testified to David's forgiveness and once for all silenced his ruthless, cynical opponents. David therefore asked that this Psalm of praise and thanksgiving be sung upon the Temple's dedication, to express gratitude to the Almighty for closing this very difficult and humiliating chapter in his life.