This is a prayer to God: “Be gracious to me, O Lord.” The poet says haters brought misery; they tormented him. But the Lord is the One who saves him.
The poet refers to the realm of death. He asks the Lord to lift him away from there, so that he may praise God's deeds. This will give him reason to rejoice again (see Psalm 9:2, Psalm 9:10). The place of rejoicing is the gates of Zion.
Over against being gripped by the gates of death, the poet is now rejoicing in the gates of God’s city, Zion: I may rejoice in your salvation.
This refrain occurs more often in the Psalms. The one who prays is in need. He complains to the Lord. The Lord saves him and he turns to praising and thanking him.
13 Be gracious to me, O LORD! See my affliction from those who hate me, O you who lift me up from the gates of death,