 It probably was not written by David. It may have been written by Ezra as he came back with the people from Babylon. We don't know who wrote it, but the exiles had returned to the promised land. Not all of them had returned. Some were still in Babylon, and they were still in captivity. And those who had returned still had struggles and issues to deal with. So it's with reference to some great deliverance of God's people that this psalm begins.