The city takes its name from the first permanent building on the site, a castle ordered to be built by Emperor Charlemagne in 808 AD. The castle was built on rocky ground in a marsh between the Alster and the Elbe as a defence against Slavic incursion. The castle was named Hammaburg, where burg means castle. The Hamma element remains uncertain also the location of this castle.
In 834, Hamburg was designated the seat of a Roman Catholic bishopric, whose first bishop Ansgar, became known as the Apostle of the North. Two years later, Hamburg was united with Bremen as the bishopric of Hamburg-Bremen. In 1529, the city embraced Lutheranism, and Hamburg subsequently received Protestant refugees from the Netherlands and France, and, in the 17th century, sephardi Jews from Portugal.
Hamburg was destroyed and occupied several times. In 845, a fleet of 600 Viking ships came up the River Elbe and destroyed Hamburg, at that time a town of around 500 inhabitants. In 1030, the city was burned down by King Mieszko II Lambertof Poland Valdemar II of Denmark raided and occupied Hamburg in 1201 and in 1214. Hamburg had several great fires, the most notable ones in 1284 and 1842. In 1842, about a quarter of the inner city was destroyed in the "Great Fire". This fire started on the night of the 4 May 1842 and was extinguished on May 8. It destroyed three churches, the town hall, and many other buildings, killed 51 people, and left an estimated 20,000 homeless. Reconstruction took more than 40 years. Hamburg was briefly annexed by Napoleon I (1810–14). Russian forces under General Bennigsen finally freed the city in 1814. During World War II Hamburg suffered a series of air raids, which killed 42,000 civilians. On February 16 1962 a severe storm caused the Elbe to rise to an all-time high, inundating one-fifth of Hamburg and killing more than 300 people.
The charter in 1189 by Frederick I "Barbarossa" granted Hamburg the status of an Imperial Free City and tax-free access up the Lower Elbe into the North Sea. In 1265, a putative forged letter was presented to or by the Rath of Hamburg. This charter, along with Hamburg's proximity to the main trade routes of the North Sea and Baltic Sea, quickly made it a major port in Northern Europe. Its trade alliance with Lübeck in 1241 marks the origin and core of the powerful Hanseatic League of trading cities. On November 8, 1266 a contract between Henry III and Hamburg's traders allowed them to establish a hanse in London. This was the first time in history the word hanse was mentioned for the trading guild Hanseatic League The first description of civil, criminal and procedural law for a city in Germany in German language, the Ordeelbook (Ordeel: sentence) was written by the solicitor of the senate Jordan von Boitzenburg in 1270. On August 10, 1410 civil commotion caused a compromise (German:Rezeß, literally meaning: withdrawal). It is the considered as the first constitution of Hamburg. In 1860, the state of Hamburg established a republican constituion. Hamburg was an independent state of the German Confederation (1815–66), the North German Confederation (1866–71), the German Empire (1871–1918) and during the period of the Weimar Republic (1919–33). In Nazi Germany Hamburg was a Gau from 1934 until 1945. After the Secon world war Hamburg was in the British Zone of Occupation and became the a state in the western part of Germany in the Federal Republic of Geermany since 1949.
During the first half of the 19th century, a patron goddess with Hamburg's Latin name Hammonia emerged, mostly in romantic and poetic references, and although she has no mythology to call her own Hammonia became the symbol of the city's spirit during this time. Hamburg experienced its fastest growth during the second half of the 19th century, when its population more than quadrupled to 800,000 as the growth of the city's Atlantic trade helped make it Europe's third-largest port. With Albert Ballin as its director, the Hamburg-America Line became the world's largest transatlantic shipping company at the turn of the century, and Hamburg was also home to shipping companies to South America, Africa, India and east Asia . Hamburg was the port for most Germans and Eastern Europeans to leave for the New World and became home to trading communities from all over the world.
The Iron Curtain — only 50 kilometres (30 mi) east of Hamburg — separated the city from most of its hinterland and further reduced Hamburg's global trade. After German reunification in 1990, and the accession of some Eastern European and Baltic States into the EU in 2004, Hamburg-Harbour and Hamburg have ambitions for regaining their positions as the region's largest deep-sea port for container shipping and its major commercial and trading centre.