The hymn Ganesha Sahasranama is found in the Ganesha purāna.
This Thousand name hymn o.
Then Ganesa appeared before Shiva in his five faced form (panchamukha Ganesha) and initiated lord Shiva with this hymn.
The story is given in detail in poorvapeethika that when lord Shiva wanted to destroy the tripurāsura - the demon king with three flying cities, but could not succeed even after enormous efforts, lord Shiva meditated on Ganesha.
It is taught to lord Shiva by lord Ganesha himself.
The hymn Ganesha Sahasranama is found in the Ganesha purāna