Shanggu Commandery (Chinese: 上谷郡) 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.