The history of its establishment is unknown, but the 'Yamashiro oi jin' to which Jugoinoge (Junior Fifth Rank, Lower Grade) was granted according to the July 21, 876 section of Nihon Sandai Jitsuroku (the sixth of six classical Japanese history texts), is thought to be this shrine, and this may be related to the fact that the Hata clan built a large dam in early the 8th century to fix the Kazuno-gawa River (Oi-gawa River, Katsura-gawa River (Yodo-gawa River System)).
創建の由諸は不詳であるが、日本三代実録の貞観(日本)18年（876年）7月21日条に従五位下が授けられたと記述のある「山代大堰神」が当社のこととされ、8世紀初頭、秦氏が大堰を作って葛野川（大堰川、桂川(淀川水系)）を改修したことと関係があるものと見られる。