By the 11th century there were two small settlements here, one where Kongens Nytorv is now, with cattle grazing on the wet shore meadows. The harbour probably lay at today's Højbro Plads. The chronicler Saxo Grammaticus called the place Portus Mercatorum, the merchants' harbour, which in the Danish of the day was Købmannahavn. Say København quickly and you can still hear it.
Around 1160 King Valdemar I handed the town to Absalon, Bishop of Roskilde, who built a castle on the islet of Slotsholmen. You can still see its remains in the cellars under Christiansborg, which houses parliament today. The town grew tenfold on herring, salted and shipped across Catholic Europe for Lent. The Hanseatic League took the town in 1368, despite the stone wall built in the century before. In 1416 Eric of Pomerania moved his seat here, and Copenhagen became the capital.
Christian IV, king from 1588 to 1648, built much of the city you see today. He built the Børsen stock exchange, the Round Tower, Rosenborg and the rows of Nyboder for navy crews. Across the harbour he laid out Christianshavn, a fortified merchants' quarter with canals on the Amsterdam model, dug by Dutch architects. The new defences were tested a decade after his death. They held against a Swedish siege in 1658 and 1659.
The 18th century hit hard. Plague in 1711 killed some 22,000 of the 65,000 inhabitants, and the fires of 1728 and 1795 burned so much of the medieval town that little of it survives. Frederiksstaden, the grand Rococo quarter, was begun in 1749. Some of the money behind the city's buildings came from slavery: between 1660 and 1806, Danish merchants, many from Copenhagen itself, shipped about 120,000 enslaved Africans to Denmark's Caribbean islands. The Yellow Palace and the Moltke Mansion were both paid for with those profits.
Britain attacked twice. In 1801 Nelson ignored an order to pull back by putting his telescope to his blind eye, and in 1807 British ships shelled the city for three days to seize the Danish fleet, burning about 1,000 buildings. Poverty followed, yet science and the arts flourished in the Golden Age, which gave the city its Neoclassical look. Denmark became a constitutional monarchy in 1849, and from 1852 people could finally build beyond the lakes. Nørrebro, Vesterbro and Frederiksberg grew fast.
German troops occupied the city from April 1940 to May 1945. When cooperation with the occupiers broke down in August 1943, the Danish navy sank its own ships in the harbour, and when the arrests of Jews began, most of them escaped to Sweden. After the war, the 1947 Finger Plan sent new housing out along five S-train lines with green land in between. The city has stuck to it. From the 1960s the young architect Jan Gehl pushed for the pedestrian streets and cycle tracks that now define the centre.
In the 1980s and 1990s the inner city ran down and the council came close to bankruptcy, as factories shut and people left for the suburbs. Service and knowledge jobs turned it round. Big projects followed one another: the Øresund Bridge to Malmö in 2000, the Metro in 2002, and in 2004 an opera house on Holmen, donated by the shipowner Mærsk Mc-Kinney Møller. By 2010 the city had recovered.