Tradition makes David the author of the Psalms. Seventy-three carry a Hebrew superscription commonly rendered "of David", but the preposition can equally mean for, belonging to, or in the style of, and the collection includes psalms that presuppose the temple he did not build and the exile that came four centuries later. The Psalter is an anthology assembled over a long period. Some of it may be very old. The Davidic ascription is traditional, and the tradition itself grew: the Greek translation attributes more psalms to him than the Hebrew does, and the Dead Sea Scrolls include a passage crediting him with three thousand six hundred psalms and other songs, which is a claim about his stature, not a bibliography.
What is striking about the narrative David, and unusual in ancient royal literature, is how badly he behaves in it. He takes Bathsheba, arranges Uriah's death, is denounced to his face by Nathan, loses control of his household, and watches his sons rebel and die. Court literature about a founding king does not normally include this. Some scholars read the succession narrative in Second Samuel as an apology, a defence written to answer accusations already circulating, that he was a bandit chief, that he served the Philistines, that he benefited a little too neatly from the deaths of Saul and Saul's heirs. On that reading, the awkward material is there because it could not be denied.
Goliath is a separate tangle: a later passage in the same book credits the killing to Elhanan of Bethlehem, and Chronicles adjusts this to Goliath's brother.