To be righteous, according to the Bible, is to be in a right relationship with God. A righteous person isn't morally perfect. On repenting for his sin, the adulterer and murderer King David (here and here) wasn't perfect, just forgiven. God's forgiveness made David right with God.