The melody for
The Star-Spangled Banner dates to around 1779 when the
Anacreontic Society decided they needed a theme song. Society President Ralph Tomlinson wrote the words to their new song,
To Anacreon In Heaven, and the music is generally ascribed to fellow Society member John Stafford Smith.
If you think that Francis Scott Key didn't have the melody in mind when he wrote Defense of Fort McHenry, think again. Prior to The Star-Spangled Banner, the most popular song in America had been Adams and Liberty; coincidentally set to the same melody, words by Robert Treat Paine and penned in 1798. And in 1806 Key wrote the poem When the Warrior Returns, in honor of an American naval victory over the Barbary pirates, and set *that* poem to the same melody as well.
Defense of Fort McHenry was written on September 14, 1814. The first printing - on handbills - was in the week immediately following. And just one week after it was written the Baltimore American newspaper quoted the verses and marked it: Tune - Anacreon In Heaven. So, the melody was attached to it right from the start. It was first publicly sung on October 19, 1814 and was already being referred to as The Star-Spangled Banner.