In a soaring eulogy to the Rev. Clementa Pinckney, President Barack Obama spoke of grace’s power to heal, the nation’s enduring racial divide, and how last week’s killings in Emanuel AME Church offered a chance for a grieving country “to find our best selves.”

The president’s 38-minute oratory Friday reached deep into history, probing the lingering wounds of slavery and desegregation while celebrating Pinckney’s long-standing devotion to his ministry and the poor.

Bringing the crowd to its feet time and again, Obama called for continued efforts to furl the Confederate battle flag.

He decried the nation’s blindness “to the unique mayhem that gun violence inflicts on us.”

And, in one of the eulogy’s most surprising moments, he paused for eight seconds, looked down somberly, and sang “Amazing Grace.”

After the services, Obama, his wife, Michelle, Vice President Joe Biden, and his wife, Jill, had private meetings with the victims’ families.

Malcolm Graham, a former North Carolina state senator and brother of victim Cynthia Hurd, said the tone was solemn. “It was yet another citizen voicing his concern for us,” Graham said. But, “In this case, it was the president of the United States. We felt real good about him doing that.” Chris Singleton, the son of Sharonda Coleman-Singleton, said the meeting was “breathtaking.”

It was the first time Obama has visited Charleston since his first presidential campaign in 2008. And it came just nine days after a white gunman was accused of opening fire in a Bible study class at Emanuel AME Church, killing Pinckney and eight others. It also came amid an increasingly heated debate over gun violence and racially charged incidents involving police, including the fatal shooting in April of Walter Scott in North Charleston.

But the mood throughout the services and afterward was joyful, even as the throng left the arena and plunged into the withering June sun. “What I like about the speech is the president didn’t spare the bitter medicine,” said North Charleston minister Nelson Rivers III, a vice president with the National Action Network. “The president went there. He went to the issue of race.”

Anticipation of the president’s visit was palpable throughout the week, and for many people, the services began hours before first light.

The Rev. Curtis Capers of Summerville was among those first to line up in Marion Square at 3:30 a.m. Three hours later, the line extended from Calhoun Street, up Meeting Street and about 100 yards around on Hutson Street. Capers, pastor of Honey Hill Baptist Church in Cottageville, said he came to pay his respects to Pinckney and other victims. “They were doing what God required them to do,” Capers said of their attendance in a Bible study class. “I believe they were ready to meet their Heavenly Father.”

Hundreds brought water, chairs, umbrellas and other supplies to help them through another hot summer morning until the TD Arena’s doors opened. By 10 a.m., lines outside the arena — steps away from Mother Emanuel — had broken down beyond the police barriers. “The gates of heaven won’t be like this,” a mourner said when he reached the arena gates. “They will be narrower, but there will be fewer.”

At 11 a.m., more than 5,900 people packed the arena, a record according to the College of Charleston, and hundreds of people were turned away. Inside, women in white dresses and men in black suits clapped their hands as a band played a joyful spiritual medley, and then “Amazing Grace.” Organizers handed out programs, which included two poems from Pinckney’s daughters, Eliana and Malana. The Rev. Norvel Goff, the interim pastor of Emanuel AME Church, drew standing ovations for his invocation: “This is no longer the TD Arena. We have transformed it into a sanctuary.”

The tributes began even as Air Force One was in the air and flying toward Charleston. Several dignitaries were introduced, including U.S. Rep. Jim Clyburn and former Secretary of State Hillary Clinton, both of whom received standing ovations. When a woman yelled “Hillary!” from the audience,” Goff, the presiding elder, reminded everyone that they were in a sanctuary.

Speakers and members of the clergy sat in a long row that stretched across the arena floor. On the far left was a sign: “Wrong Church, Wrong People, Wrong Day.” One by one, the speakers went to the lectern, adorned with a purple church banner. They spoke of the potential of the tragedy to create positive change.

“His sacrifice must lead to reconciliation,” said state Sen. Gerald Malloy, who represents counties in the Pee Dee. “Clementa Pinckney’s last act as a Christian and as a senator was to open his doors to someone he did not know.” The Rev. John R. Bryant, senior bishop of the AME Church, had people on their feet after he said, “Someone should have told that young man ... he wanted to start a race war. But he came to the wrong place.”

They stood again when Bryant credited the governor for her bold move to remove the flag from the Statehouse grounds. “Joy comes in the morning,” Bryant said. “Touch the person next to you and say, ‘Good morning.’ ”

Anticipation grew as the speakers went over their allotted time and word spread that the president and vice president had arrived.

Since he was elected the nation’s first African-American president, Obama has been called on frequently to serve as consoler-in-chief: Three months after Obama was sworn in, a man killed 13 people at an immigration center in Binghamton, New York; seven months later, an Army psychiatrist fatally shot 13 people at Fort Hood, Texas; a year later, a 22-year-old opened fire at a Tucson supermarket, killing six and wounding 11, including U.S. Rep. Gabrielle Giffords; six months later, a man shot and killed 12 people in an Aurora, Colo., movie theater; in 2012, a gunman shot 20 first-graders and six adults in Newtown, Conn.

When Obama took the podium Friday afternoon, he quickly introduced the eulogy’s central theme: Pinckney, the president said, was “a man who believed in things not seen, a man who believed there were better days ahead.”

Noting Pinckney’s smile and “reassuring baritone,” Obama described Pinckney’s remarkable career. “He was in the pulpit by 13, pastor by 18, public servant by 23,” and how as a state senator for Allendale, Beaufort, Charleston, Colleton, Hampton and Jasper counties, he “represented a sprawling swath of Lowcountry, a place that has long been one of the most neglected in America, a place still racked by poverty and inadequate schools, a place where children can still go hungry and the sick can go without treatment — a place that needed someone like Clem.”

Pinckney, he added, “embodied a politics that was neither mean nor small. He conducted himself quietly and kindly and diligently.”

Obama then shifted toward the killings and their surprising aftermath. “To the families of the fallen, the nation shares in your grief. Our pain cuts that much deeper because it happened in a church. The church is and always has been the center of the African-American life.”

The president touched on Charleston’s response, led by the families of the victims.

“The alleged killer could never have anticipated how the families would respond,” he said. “Amid unspeakable grief,” they spoke about forgiveness and love. The arena came to its feet, and an organ played a few notes. “Blinded by hatred, he failed to comprehend what Rev. Pinckney so well understood — the power of God’s grace.”

The crowd stood for another ovation as he spoke of Mother Emanuel — “a church built by blacks seeking liberty, burned to the ground because its founders sought to end slavery only to rise up again, a phoenix from these ashes.” Then, he took aim at the Confederate flag.

“For too long, we were blind to the pain that the Confederate flag stirred into many of our citizens,” he said, lauding Gov. Nikki Haley’s call to remove the flag from the Statehouse grounds. “As we all have to acknowledge, the flag has always represented more than ancestral pride. For many, black and white, that flag was a reminder of systematic oppression and racial subjugation. We see that now.”

He said putting the Confederate flag in its proper place was a first step toward healing the nation’s wounds, but “I don’t think God wants to stop there.” He spoke about how racial bias “can infect us even when we don’t realize it,” drawing one of the biggest cheers when he described the “subtle impulse to call Johnny back for a job interview but not Jamal.”

Near the end, he returned to the personal impact the shootings had on him. “An open heart. That’s what I felt this week. An open heart.” He spoke of grace, and of how “if we can find that grace, anything is possible.” His voice lowered then, and he paused and led the arena in “Amazing Grace.” The crowd erupted in song and cheers. And his voice rose over the cheers as he said,

“Clementa Pinckney found that grace ...

“Cynthia Hurd found that grace ...

“Susie Jackson found that grace ...

“Ethel Lance found that grace ...

“DePayne Middleton Doctor found that grace ...

“Tywanza Sanders found that grace ...

“Daniel L. Simmons Sr. found that grace ...

“Sharonda Coleman-Singleton found that grace ...

“Myra Thompson found that grace.”

Robert Behre, Glenn Smith, Jennifer Berry Hawes, Hanna Raskin, Brenda Rindge, Christina Elmore, Melissa Boughton and Jeff Hartsell contributed to this report.