The biblical text tells us how to read this psalm. Whether or not we agree with those who see the words “build the walls of Jerusalem” at the end as a reference to the need to (re)build after the destruction of 586, and so (perhaps along with other reasons) see the psalm as actually written later, we are told to read the psalm against the story of David’s sin and Nathan’s visit
NB: the headings like <To the leader. A Psalm of David, when the prophet Nathan came to him, after he had gone in to Bathsheba.> are often printed as if they had less authority than the big bold headings Bible publishers add – but they are part of the Bible and the publishers headings are NOT!
So that’s how I’ll read it. In doing so I’ll comment on details of wording, as well as on how this psalm fits into the big picture of the Bible.