Psalm 31 is a lament by a faithful person who seeks help from God because trouble and beset by enemies who want to do him harm. This lament could apply to all faithful people, not just David.
1-2 – The singer uses metaphors like rock, fortress, and refuge to demonstrate his dependence and trust in God. It is God’s righteousness, that is God’s faithfulness to his covenant promises that is the ground for confidence and assurance of God’s help.
3-8 – Now the singer recounts previous experiences where God has rescued him from enemies that would do him harm, and so the singer rejoices and is glad. Note that verse 5 states “into your hand I commit my spirit,” which Jesus recited on the cross. Like Jesus, the innocent sufferer trusts in God for his vindication. In verse 6 the singer says that he hates the wicked and has not part with them.
9-13 – Here the singer states that he is once again in distress. His adversaries plot to take his life. The singer confesses that he is a sinner.
14-18 – The singer expresses trust in God for deliverance based upon his prior experience and based upon covenant promises of God in the covenant to sustain the righteous. “I trust in you, O LORD.” “My times are in your hand; rescue me from the hand of my enemies and from my persecutors!” “O LORD, let me not be put to shame, for I call upon you.” The faithful righteous people are those who trust in God, believe his word, and seek to please him. The wicked are everyone else, and the wicked seek to destroy the faithful.
19-24 – the singer closes with assurance that God who made covenant promises and who kept the promises to his faithful people in the past will continue to do so.