Regardless of size, many walled cities in ancient China contained a temple dedicated to one or more immortal or god as the spirit(s) or protector(s) of the city.
The temple's surrounding area and vicinity is a large commercial district that hosts an array of shops, restaurants, teahouses, as well as annual temple fair events
The City God Temple in Shanghai originated as the Jinshan God Temple, dedicated to the spirit of Jinshan, or "Gold Mountain", an island off the coast of Shanghai. It was converted into a City God Temple in 1403, during the Yongle era of the Ming dynasty.
During the Qing Dynasty, the temple grew popular. Residents of the old city as well as nearby areas visited the temple to pray for good fortune and peace. The temple reached its largest extent in the Daoguang era. The popularity of the temple also led to many business to be set up in the area, turning the surrounding streets into a busy marketplace.
In 1951, the Board of Trustees of the City God Temple was dissolved, and the temple was handed over to the Shanghai Taoist Association and made into a Taoist center. The institution made changes to the temple, removing statues representing folk Underworld personalities such as Yama, the judge of the dead, and placing an emphasis on Taoist spirituality instead.
During the Cultural Revolution, the temple was closed down and used for other purposes. For many years, the main hall was used as a jewellery shop. In 1994, the temple was restored to its former use as a temple, with resident Taoist priests. The Temple, together with nearby Yuyuan Garden and the surrounding streets, are now part of a large pedestrian zone dedicated to restaurants and retail.
A complete restoration of the City God Temple took place between 2005 and 2006. In October 2006 the place of worship was reopened and reconsecrated by Taoist clergymen.
 The city gods
The temple is dedicated to three city gods:
- Huo Guang (? - 68 BC) was a famous Han Dynasty Chancellor. He is remembered for his role in deposing one young emperor and replacing him with another. Huo Guang was the original City God for the County of Shanghai from the Yuan Dynasty.
- Qin Yubo (1295–1373) lived in Shanghai and served in the late Yuan Dynasty civil service. When the Hongwu Emperor founded the Ming Dynasty, he resisted two summons to serve at the court. He finally relented, and served in various roles including chief Imperial examiner. After his death, he was anointed City God of Shanghai by the Hongwu Emperor.
- Chen Huacheng (1776–1842) was a Qing Dynasty general, responsible for the defence of Shanghai during the First Opium War. He vowed to defend the Yangtze to the death, and was killed in battle against the British.
 New City God Temple
During the Second Sino-Japanese War, the old city was occupied by the Japanese while, initially, they left the foreign concessions alone. As a result, worshippers from the concessions were cut off from the temple. As a response, local merchants built a new temple and attached market place near what is today Yan'an Road and Jinling Road, in the Shanghai International Settlement. This was known as the "New City God Temple". After the end of World War II, the New City God Temple waned in popularity as worshippers shifted back to the Old City God Temple. The new temple and markets were demolished in 1972. However, the "New City God Temple" remains in use referring to the locality around the site of that temple.