People have lived around the harbour since the late 13th century. A camp at Kaikai Beach, north of today's city, dates from about then, and by the 14th century there were large, permanent moa-hunter sites across the area. Later came fortified villages, pā, such as Pukekura at Taiaroa Head, built about 1650. A sealer who visited in the late 1820s recorded that the harbour's villages were the biggest and longest-established in the south. A Māori meeting-house at Ōtākou gave the peninsula its name. The city has three Ngāi Tahu marae.
Sealers began visiting from the start of the 19th century. William Tucker was the first European settler nearby, in 1815. In 1831 the Weller brothers opened a whaling station at Ōtākou, and by the late 1830s the harbour was an international whaling port. Introduced illnesses such as measles cut the Māori population hard. The whales were hunted almost to extinction, and the station closed in the 1840s.
The city proper began in 1848, when lay members of the Free Church of Scotland founded it at the head of the harbour. The surveyor Charles Kettle was told to copy Edinburgh. His plan did not always survive the hills, which is why some streets are grand and some are odd. Many street names repeat Edinburgh's, though in a different order. Around 12,000 Scots had arrived by the end of the 1850s. The minister Thomas Burns, whose uncle was the poet, was among them. The Octagon still has a statue of Robert Burns, put up in 1887.
Gold changed everything. In 1861 it turned up at Gabriel's Gully, inland to the south-west, and people poured in. By 1865 Dunedin was the largest city in New Zealand by population, and by 1874 it held 29,832 people with its suburbs, the most in the country. The newcomers included Irish, Italians, Lebanese, French, Germans, Jews and Chinese. Dunedin has the oldest Chinese community in New Zealand.
The money built institutions. The Otago Museum opened in 1868 and the University of Otago, the oldest in the country, in 1869. The Union Company, founded here in 1875, grew into the largest shipping line in the Southern Hemisphere, and in 1881 a ship named Dunedin carried the first commercially successful load of frozen meat to Britain. What you see on the streets today is the same wealth in stone: Lawson's First Church, Knox Church, and work by the architects Maxwell Bury and F. W. Petre.
By 1900 Dunedin was no longer the biggest city in the country. A generation that disliked all things Victorian pulled down many old buildings, among them William Mason's Stock Exchange in 1969. The population shrank between 1976 and 1981.
From about 1990 the city turned the old stock into an asset. Decline had steadied, and Dunedin began to sell itself as a heritage city, and its main streets were redone in Victorian style. In 2014 it became a UNESCO City of Literature, the first New Zealand city appointed to the Creative City network.