Shanggu Commandery was a commandery in imperial China from the Warring States period to Tang dynasty. It was located in present-day Hebei and Beijing. The commandery was established by the state of Yan for the defense against the Xiongnu. In Western Han dynasty, it administered 15 counties, including Juyang, Quanshang, Pan, Jundu, Juyong, Goumao, Yiyu, Ning, Changping, Guangning, Zhuolu, Qieju, Ru, Nüqi and Xialuo. Guangyang Commandery was merged into Shanggu in Emperor Guangwu of Han's reign, but was restored in 96 AD, and two former Shanggu counties – Jundu and Changping – was transferred there. Later during Eastern Han, only 8 counties remained, namely Juyang, Pan, Ning, Guangning, Juyong, Goumao, Zhuolu and Xialuo. The total number of households was 36,008 in 2 AD, while the population was 117,762. In 140 AD, the population was 51,204, and the households numbered 10,352. In early Eastern Han, some of the Wuhuan people settled within the empire's northern borders. Ning County in Shanggu became the seat of an office known as the "Colonel protecting the Wuhuan", which was in charge of relationships with the Wuhuan and Xianbei tribes and had the rank equivalent to that of a commandery's Grand Administrator. A seasonal market was also established there. During Emperor Wu of Jin's reign, a new Guangning Commandery was established. Shanggu had 2 counties and 4,070 households, while Guangning administered 3 counties and 3,950 households. During the Sixteen Kingdoms era, the area was successively held by Later Zhao, Former Yan, Former Qin and Later Yan. Later, Shanggu became part of Northern Wei until the Xiaochang era when it was lost during the Rebellion of Six Garrisons. It covered 2 counties – Pingshu and Juyong – and had 3,093 households. The commandery was abolished in early Sui dynasty. In Sui and Tang dynasties, Shanggu Commandery became an alternative name for Yi Prefecture. It consisted of 6 counties: Yi, Rongcheng, Suicheng, Laishui, Mancheng and Wuhui. The population was 258,779 in 742 AD, in 44,230 households.