The first verse of Psalm 138 is clearly stating David as author of the Psalm. In verse 2 it says:
I will bow down toward your holy temple and will praise your name for your unfailing love and your faithfulness, for you have so exalted your solemn decree that it surpasses your fame.
But the temple was not built in the time of David. What is going on here? Is the temple refering to the tabernacle? Or is the Psalm written by some other David? Or someone that is using the kings name?
It's worth noting that there are also others Psalms attributed to David which mentions a temple, such as; Ps 5:8, Ps 24:3.