In Psalm 26, the singer boasts of being perfectly righteous and faithful to the Lord; he (presumably) expects to be rewarded. In Psalm 27, the singer is absolutely confident of God's protection. We know, of course, that the real world doesn't work that way. Very often, the wicked prosper and the righteous suffer. Being pious isn't any protection at all. This is what we call magical thinking. Praying, purporting to believe, performing the rituals, and obeying the commandments just aren't going to protect you from the dangers of the world. But many people would like to think so.
A Psalm of David.
26 Vindicate me, O Lord,
for I have walked in my integrity,
and I have trusted in the Lord without wavering.
2 Prove me, O Lord, and try me;
test my heart and my mind.
3 For thy steadfast love is before my eyes,
and I walk in faithfulness to thee.[a]
4 I do not sit with false men,
nor do I consort with dissemblers;
5 I hate the company of evildoers,
and I will not sit with the wicked.
6 I wash my hands in innocence,
and go about thy altar, O Lord,
7 singing aloud a song of thanksgiving,
and telling all thy wondrous deeds.
8 O Lord, I love the habitation of thy house,
and the place where thy glory dwells.
9 Sweep me not away with sinners,
nor my life with bloodthirsty men,
10 men in whose hands are evil devices,
and whose right hands are full of bribes.
11 But as for me, I walk in my integrity;
redeem me, and be gracious to me.
12 My foot stands on level ground;
in the great congregation I will bless the Lord.
- Psalm 26:3 Or in thy faithfulness
A Psalm of David
27 The Lord is my light and my salvation;
whom shall I fear?
The Lord is the stronghold[a] of my life;
of whom shall I be afraid?
2 When evildoers assail me,
uttering slanders against me,[b]
my adversaries and foes,
they shall stumble and fall.
3 Though a host encamp against me,
my heart shall not fear;
though war arise against me,
yet I will be confident.
4 One thing have I asked of the Lord,
that will I seek after;
that I may dwell in the house of the Lord
all the days of my life,
to behold the beauty of the Lord,
and to inquire in his temple.
5 For he will hide me in his shelter
in the day of trouble;
he will conceal me under the cover of his tent,
he will set me high upon a rock.
6 And now my head shall be lifted up
above my enemies round about me;
and I will offer in his tent
sacrifices with shouts of joy;
I will sing and make melody to the Lord.
7 Hear, O Lord, when I cry aloud,
be gracious to me and answer me!
8 Thou hast said, “Seek ye my face.”
My heart says to thee,
“Thy face, Lord, do I seek.”
9 Hide not thy face from me.
Turn not thy servant away in anger,
thou who hast been my help.
Cast me not off, forsake me not,
O God of my salvation!
10 For my father and my mother have forsaken me,
but the Lord will take me up.
11 Teach me thy way, O Lord;
and lead me on a level path
because of my enemies.
12 Give me not up to the will of my adversaries;
for false witnesses have risen against me,
and they breathe out violence.
13 I believe that I shall see the goodness of the Lord
in the land of the living!
14 Wait for the Lord;
be strong, and let your heart take courage;
yea, wait for the Lord!