confirm with your doctor is this is constipation
. get the perianal area examined for any painful condition like sores, rash
etc. hard stools can commonly cause such pain, you may ask your doctor to start some calcium bicarbonate supplements, that in addition may help soften the stools.
Dr S Kondekar
Asso Prof Pediatrics
TN Medical College Mumbai