What does Psalm 18:20 mean?
David credits the Lord with rescuing him in accordance with David's righteousness. This does not imply that David was sinless, or perfect. Rather, David sees his rescue as a reward from the Lord for his integrity.In a sense, there are parallels between this idea and the account of Daniel. Several centuries after David, Daniel had faithfully honored the Lord, and the Lord rewarded him by rescuing him from the lions. God rescued Daniel from the lions' den, whereas his enemies became meals for the lions (Daniel 6:21–24).
Prophetically, David's words here apply to the Messiah, the Lord Jesus Christ. He was thoroughly righteous in word and deed (1 Peter 2:22), and three days after He died for our sins, God responded by raising Him from the dead (Romans 4:24–25). In Psalm 16:10 David declares: "For you will not abandon my soul to Sheol, or let your holy one see corruption." At Pentecost, when Peter addressed a crowd of Jews, he applied David's words in Psalm 16 to Christ. He said, "He [David] foresaw and spoke about the resurrection of Christ, that he was not abandoned to Hades, nor did his flesh see corruption" (Acts 2:31).