David wrote a lot of Psalms during his time of struggles and battles with Saul. Not many people can give praises to the Lord like David did when experience what David was experiencing. Some Psalms David wrote was cursing the enemy and crying for justice. David was being betrayed by his friends and soldiers which could endanger his life. He was scared and worried, but instead of being worried, he cried out to Lord.
Also, I found astonished when I read Psalms 51, which was written when the prophet Nathan came to accuse David after he had committed adultery with Bathsheba. He admitted his sin to God and ask for forgiveness. If I were him, I would be to shame to even think of God. But David knew he is sinful, and most importantly, God did ask Nathan to confront David about his behavior. What if God did not talk to David though Nathan?
Psalm 51: 1-2 NIV “Have mercy on me, O God, according to your unfailing love; according to your great compassion blot out my transgressions. Wash away all my iniquity and cleanse me from my sin.”
Psalm 51:17 NIV “My sacrifice, O God, is a broken spirit; a broken and contrite heart you, God, will not despise.”