This psalm is titled A Song of Ascents. Of David. Psalm 122:4 mentions that the pilgrims gathered in Jerusalem to give thanks. Here we see David leading Israel in giving thanks to God for past help and expressing confidence in His continuing help.
Despite the attribution to David, several commentators connect this psalm with exiles returning from Babylon. James Montgomery Boice answered this well: “The expressions of the psalm (‘when men attacked us,’ ‘swept us away,’ ‘escaped like a bird’) sound more like a military attack and deliverance from it than captivity.” It is best to keep the connection with David, considering it an earnest plea for Israel to thank God for deliverance past and present.
“In the year 1582, this Psalm was sung on a remarkable occasion in Edinburgh. An imprisoned minister, John Durie, had been set free, and was met and welcomed on entering the town by two hundred of his friends. The number increased till he found himself in the midst of a company of two thousand, who began to sing, as they moved up the long High Street, ‘Now Israel may say,’ etc. They sang in four parts with deep solemnity, all joining in the well-known tune and Psalm. They were much moved themselves, and so were all who heard; and one of the chief persecutors is said to have been more alarmed at this sight and song than at anything he had seen in Scotland.” (Horatius Bonar, cited in Charles Spurgeon)