The killer, who police named as Jesse Van Rootselaar, died by suicide after the shooting on Tuesday, local time, in Tumbler Ridge, a remote community in the Pacific province of British Columbia.
Police revised the death toll down to nine from the initially reported 10. More than 25 people were wounded.
"Police had attended that (family) residence on multiple occasions over the past several years, dealing with concerns of mental health with respect to our suspect," said Deputy Commissioner Dwayne McDonald, commander of the Royal Canadian Mounted Police in British Columbia.
McDonald said Van Rootselaar, who was born male but began to identify as a female six years ago, had first killed her mother, 39, and 11-year-old step-brother at the family home.
She then went to the school, where she shot a 39-year-old woman teacher as well as three 12-year-old female students and two male students, one aged 12 and one aged 13.
"We do believe the suspect acted alone ... it would be too early to speculate on motive," he told a press conference.
At the school, one victim was found in a stairwell and the rest, McDonald believed, were found in the library. The suspect was not related to any of the victims at the school, he said.
"There is no information at this point that anyone was specifically targeted," McDonald said.
Police recovered a long gun and a modified handgun.
Earlier in the day a visibly upset Prime Minister Mark Carney promised Canadians would get through what he called a "terrible" shooting.
Carney, who has postponed a trip to Europe, said he had ordered flags on all government buildings be flown at half-mast for the next seven days.
"We will get through this. We will learn from this," he told reporters, at one point looking close to tears.
"But right now, it's a time to come together, as Canadians always do in these situations, these terrible situations, to support each other, to mourn together and to grow together."
Several prominent world leaders sent messages of condolence. King Charles, Canada's head of state, said he was "profoundly shocked and saddened" by the deaths.
The shooting ranks among the deadliest in Canadian history. Canada has stricter gun laws than the United States, but Canadians can own firearms with a licence.
In April 2020, a 51-year-old man disguised in a police uniform and driving a fake police car shot and killed 22 people in a 13-hour rampage in the Atlantic province of Nova Scotia, before police killed him at a gas station.
In Canada's worst school shooting, in December 1989, a gunman killed 14 female students and wounded 13 at the Ecole Polytechnique in Montreal, Quebec, before taking his own life.
"There's not a word in the English language that's strong enough to describe the level of devastation that this community has experienced," said Larry Neufeld, a local provincial legislator.
"It's going to take a significant amount of effort and a significant amount of courage to repair that terror," he told CBC News.
With AP
Lifeline 13 11 14
beyondblue 1300 22 4636