How are certain verses that describe God as always being there in times of need such as Psalm 46:1 or Psalm 145:14 compatible with verses such as Psalm 10:1 and Psalm 22:1-2 that describe God hiding himself and forsaking David.
God is constant; man is not. The Psalms are reflections of the hearts of the persons who composed them. David in faith defeated Goliath and in folly slept with a friend's wife and had him killed. He worshiped God wholeheartedly and spared the life of his enemy King Saul, and he also was chased for years and despaired of life. One son became a wise leader, while another started a civil war. The circumstances of his life constantly changed. Each Psalm was written from a different emotional place. His worship was colored by his emotions. He felt elated over victory. He felt angry over injustice. He felt guilty over sin (like Psalm 51). The Psalms are outpourings of the heart which reveal the inner turmoils and beliefs of David and the other writers.
When the Psalmist expresses unmet expectations of God, disappointment, anger, despair, or confusion, they are being honest about what they believe, but their picture of God may be distorted or incomplete. However, notice that many Psalms show a progression. Psalm 22 begins in a dark place, but ends with an affirmation of faith that the God who is not at present showing up and remedying the situation will come in time. This shows the emotional and spiritual growth of the one who is praying and worshiping God. Their trials are increasing their faith.
This same sort of contradiction is found in Proverbs and Ecclesiastes, each of which are traditionally believed to have been written, curated, or at least contributed to by Solomon. The first confidently expresses the blessings that accrue to the person that fears God, while the second expresses confusion over how often injustice and futility prevail in life, and how people who do not fear God often flourish, while the wise are forgotten.
Returning to Psalm 22:
19 But you, Lord, do not be far from me.
You are my strength; come quickly to help me.
20 Deliver me from the sword, my precious life from the power of the dogs.
21 Rescue me from the mouth of the lions; save me from the horns of the wild oxen.
22 I will declare your name to my people; in the assembly I will praise you.
23 You who fear the Lord, praise him!
All you descendants of Jacob, honor him!
Revere him, all you descendants of Israel!
24 For he has not despised or scorned the suffering of the afflicted one;
he has not hidden his face from him but has listened to his cry for help.
Between verse 21 and verse 22, we have a transition from supplication to praise. Have the Psalmist's fortunes suddenly reversed? No, what has changed is their attitude. It is one thing to praise God for what he has done, and quite another to praise Him for what you are confident he will do. The first shows simple gratitude, while the second shows faith.
Having seen my fair share of distress in this life, I know that there are times in the midst of trouble when God feels far off, and times when he feels near. The only difference is our perception: the eyes of faith can see that God is near. The nearness of God is not in question, it is how near we will allow ourselves to be to him.
If Psalms 10, 22, 46 and 145 were indeed written by King David or by members of his court, this would be a strange contradiction. The same voice can not say on the one hand that God is ever-present (46:1) and is always ready to help those who fall (145:14), yet that God hides himself in times of trouble (10:1) and that he has forsaken me (22:1).
Mark S. Smith says in 'Taking Inspiration: Authorship, Revelation and the Book of Psalms', published in Psalms and Practice (edited by Stephen Breck Reid), page 245, the scholarly consensus is that the superscriptions, from which we assume Davidic authorship, are prose additions to the prior written poems. He cites Brevard S. Childs, to say that "the Psalms titles do not appear to reflect independent historical tradition but are the result of an exegetical activity which derived its material from within the text itself.
Smith says (ibid,page 249) Davidic authorship of these psalms is highly problematic. He says the psalms "of David" for the most part could not have been written by David since their grammar points to a later period (unless massive rewriting of psalm texts is assumed, an assumtion he dismisses on textual evidence). These psalms actually contain evidence that point to the time of the Babylonian Exile.
Smith says (ibid, page 261), that many psalms, such as Psalm 22, recount a lament leading to a divine answer which provokes the act of thanksgiving. Here, Psalm 22:1-18 is the lament, verses 19-21 contain a certainty that God will help, then the remainder of Psalm 22 praises God for the help that is forthcoming. In the same way, Psalm 10:1-11 is the lament, verse 14 assures us that God is committed to the poor and the fatherless, verse 15 says what God will do, then the thanksgiving is found in verses 16-18. In contradiction to Psalm 10:1, verses 17-18 say God has heard the desire of the humble and will stop their oppression:
Psalm 10:17-18: LORD, thou hast heard the desire of the humble: thou wilt prepare their heart, thou wilt cause thine ear to hear: To judge the fatherless and the oppressed, that the man of the earth may no more oppress.
The psalms were written for liturgy and should therefore be read as liturgy.