The two Chinese characters in the city's name are (shàng) 'above' and (hǎi) 'sea' which together literally mean 'Upper Sea'. The earliest occurrence of this name dates from the Song Dynasty (11th century), at which time there was already a river confluence and a town with this name in the area. There are disputes as to exactly how the name should be interpreted, but official local histories have consistently said that it signifies 'The upper reaches of the sea'. Due to the changing coastline, Chinese historians have concluded that in the Tang Dynasty Shanghai was literally on the sea, hence the origin of the name. A more poetic name for Shanghai switches the order of the two characters, Hǎishàng (), and is often used for terms related to Shanghainese art and culture.
Shanghai is commonly abbreviated in Chinese as Hù (simp: /trad: ). This character appears on all motor vehicle license plates issued in the municipality today. This is derived from Hu Du (simp: /trad: ), the ancient name for the lower section of the Suzhou Creek as it enters the sea, the same section which is today regarded as the lower section of the Huangpu River. The character Hu is often combined with Song to form the name Songhu (). Song comes from another ancient name for the Suzhou Creek, the Song River (after which the town Songjiang is named). For example, the 1937 Battle of Shanghai is known in Chinese as the Battle of Songhu. A second abbreviation for Shanghai is Shēn (), derived from the name of Chunshen Jun (), a nobleman and locally revered hero of the Chu Kingdom in the third century BC whose territory included the Shanghai area. Sports teams and newspapers in Shanghai often use the character Shēn () in their names. Shanghai is also commonly called Shēnchéng (, "[Walled] city of Shēn").
Another early name for Shanghai was Huating (). In 751 AD, during the mid-Tang Dynasty, Huating County was established at modern-day Songjiang, the first county-level administration within modern-day Shanghai. Today, Huating is most often encountered as the name of a four-star hotel in the city.
During the Song Dynasty (AD 960–1279) Shanghai was upgraded in status from a village (村) to a market town (镇) in 1074, and in 1172 a second sea wall was built to stabilise the ocean coastline, supplementing an earlier dike. From the Yuan Dynasty in 1292 until Shanghai officially became a city for the first time in 1297, the area was designated merely as a county (縣) administered by the Songjiang prefecture.
Two important events helped promote Shanghai's development in the Ming Dynasty. A city wall was built for the first time in 1554, in order to protect the town from raids by Japanese pirates. It measured 10 metres high and 5 kilometres in circumference. During the Wanli reign (1573–1620), Shanghai received an important psychological boost from the erection of a City God Temple (城隍庙) in 1602. This honour was usually reserved for places with the status of a city, such as a prefectural capital (府), and was not normally given to a mere county town (镇) like Shanghai. The honour was probably a reflection of the town's economic importance, as opposed to its low political status.
During the Qing Dynasty, Shanghai became one of the most important sea ports in the Yangtze Delta region. This was a result of two important central government policy changes. First of all, Emperor Kangxi (1662–1723) in 1684 reversed the previous Ming Dynasty prohibition on ocean going vessels, a ban that had been in force since 1525. Secondly, in 1732 Emperor Yongzheng moved the customs office for Jiangsu province (; see Customs House, Shanghai) from the prefectural capital of Songjiang city to Shanghai, and gave Shanghai exclusive control over customs collections for Jiangsu Province's foreign trade. As a result of these two critical decisions, Professor Linda Cooke Johnson has concluded that by 1735 Shanghai had become the major trade port for all of the lower Yangtze River region, despite still being at the lowest administrative level in the political hierarchy.
International attention to Shanghai grew in the 19th century due to its economic and trade potential at the Yangtze River. During the First Opium War (1839–1842), British forces temporarily held the city. The war ended with the 1842 Treaty of Nanjing, opening the treaty ports, Shanghai included, for international trade. The Treaty of the Bogue signed in 1843, and the Sino-American Treaty of Wangsia signed in 1844 together allowed foreign nations to visit and trade on Chinese soil, the start of the foreign concessions.
In 1854, the Shanghai Municipal Council was created to manage the foreign settlements. In 1860–1862, during a civil war Shanghai had been invaded twice. In 1863, the British settlement, located to the south of Suzhou creek (Huangpu district), and the American settlement, to the north of Suzhou creek (Hongkou district), joined in order to form the International Settlement. The French opted out of the Shanghai Municipal Council, and maintained its own French Concession, located to the south of the International Settlement, which still exists today as a popular attraction. Citizens of many countries and all continents came to Shanghai to live and work during the ensuing decades; those who stayed for long periods — some for generations — called themselves "Shanghailanders". In the 1920s and 1930s, almost 20,000 so-called White Russians and Russian Jews fled the newly established Soviet Union and took up residence in Shanghai. These Shanghai Russians constituted the second-largest foreign community. By 1932, Shanghai had become the world's fifth largest city and home to 70,000 foreigners. In the 1930s, some 30,000 Jewish refugees from Europe arrived in the city.
The Sino-Japanese War concluded with the Treaty of Shimonoseki, which elevated Japan to become another foreign power in Shanghai. Japan built the first factories in Shanghai, which were soon copied by other foreign powers. Shanghai was then the most important financial centre in the Far East. All this international activity gave Shanghai the nickname "the Great Athens of China"
Under the Republic of China (1911–1949), Shanghai's political status was finally raised to that of a municipality on July 14, 1927. Although the territory of the foreign concessions was excluded from their control, this new Chinese municipality still covered an area of 828.8 square kilometers, including the modern-day districts of Baoshan, Yangpu, Zhabei, Nanshi, and Pudong. Headed by a Chinese mayor and municipal council, the new city governments first task was to create a new city centre in Jiangwan town of Yangpu district, outside the boundaries of the foreign concessions. This new city centre was planned to include a public museum, library, sports stadium, and city hall.
On January 28, 1932, Japanese forces struck and the Chinese resisted, fighting to a standstill; a ceasefire was brokered in May. The Battle of Shanghai in 1937 resulted in the occupation of the Chinese administered parts of Shanghai outside of the International Settlement and the French Concession. The International Settlement was occupied by the Japanese on 8 December 1941 and remained occupied until Japan's surrender in 1945, during which time war crimes were committed.
On May 27, 1949, the Communist People's Liberation Army took control of Shanghai, which was one of only three former Republic of China (ROC) municipalities not merged into neighbouring provinces over the next decade (the others being Beijing and Tianjin). Shanghai underwent a series of changes in the boundaries of its subdivisions, especially in the next decade. After 1949, most foreign firms moved their offices from Shanghai to Hong Kong, as part of an exodus of foreign investment due to the Communist victory.
During the 1950s and 1960s, Shanghai became an industrial centre and centre for radical leftism; the leftist Jiang Qing and her three cohorts, together the Gang of Four, were based in the city. Yet, even during the most tumultuous times of the Cultural Revolution, Shanghai was able to maintain high economic productivity and relative social stability. In most of the history of the People's Republic of China (PRC), in order to funnel wealth to the rural areas, Shanghai has been a comparatively heavy contributor of tax revenue to the central government. This came at the cost of severely crippling Shanghai's infrastructural and capital development. Its importance to the fiscal well-being of the central government also denied it economic liberalisations begun in 1978. Shanghai was finally permitted to initiate economic reforms in 1991, starting the massive development still seen today and the birth of Lujiazui in Pudong.
Shanghai sits on the Yangtze River Delta on China's eastern coast, and is roughly equidistant from Beijing and Hong Kong. The municipality as a whole consists of a peninsula between the Yangtze and Hangzhou Bay, mainland China's second largest island Chongming, and a number of smaller islands. It is bordered on the north and west by Jiangsu Province, on the south by Zhejiang Province, and on the east by the East China Sea. The city proper is bisected by the Huangpu River, a tributary of the Yangtze. The historic centre of the city, the Puxi area, is located on the western side of the Huangpu, while the newly developed Pudong, containing the central financial district Lujiazui, was developed on the eastern bank.
The vast majority of Shanghai's land area is flat, apart from a few hills in the southwest corner, with an average elevation of . The city's location on the flat alluvial plain has meant that new skyscrapers must be built with deep concrete piles to stop them sinking into the soft ground. The highest point is at the peak of Dajinshan Island at . The city has many rivers, canals, streams and lakes and is known for its rich water resources as part of the Taihu drainage area.