Mayor Bill de Blasio told congregants of a Harlem church on Sunday that though the nation and city have a history of racism to overcome, progress has been made and hope must not be abandoned.

His remarks came amid heightened tensions following several high-profile fatal shootings of black men and police officers last week in Louisiana, Minnesota and Texas.

“We have no choice but to build something better in our time,” the Democratic mayor said, standing alongside first lady Chirlane McCray. “And let this city be an example.”

He spoke at Bethel Gospel Assembly, a nondenominational predominantly black church.

“We have to be honest about structural racism,” said de Blasio, who last week acknowledged that he fears for his biracial children in the current climate.

The country has “a history of division, a history of discrimination” that began 400 years ago, and recent incidents could very easily lead to cynicism, he said.

But, de Blasio added, “The last thing that we should do is walk away, retreat, fail to believe that we can make the next level of progress.”

Later, at a packed St. Patrick’s Cathedral on Fifth Avenue, the mayor said leaders in the NYPD are addressing these very issues.

“This city has to become an example. We’ve come a long way. We’re far, far from perfect, but we’ve come a long way,” he said.

“In this city we aspire to a different and better relationship between police and community. We think it can happen.” He thanked NYPD Commissioner William Bratton and Chief of Department James O’Neill, adding that they are “creating a new approach.”

“Their common humanity the driving force — not assumptions or stereotypes, not biases,” he said. “It will take real work . . . but it can be done.”

De Blasio spoke for about seven minutes as services were about to end. Cardinal Timothy Dolan said afterward that the mayor had asked to speak and was told he was always welcome.

In his sermon Sunday, Dolan called for calm and peace and reminded the congregation that people turn to God for answers during times of crisis. “It’s almost innate that we go to the Lord and that’s not a bad thing,” he said. “And ultimately that’s what’s going to get us through.”

The mayor told the congregation there has to be “a deep mutual respect” between police and community. “An attack on our police is an attack on all of us,” he said.

Earlier at Bethel, de Blasio also emphasized trust and respect must be restored between police and the community. New York City has begun the work of improving relations by emphasizing neighborhood policing and using body cameras, he said.

De Blasio said there was a time when every village had “guardians” and there was “no blue wall.” The country and city must return to that time, he said.

Members of the Baptist congregation offered de Blasio and McCray prayers and the church’s pastor, Bishop Carlton T. Brown, echoed the mayor’s message.

“All policemen are not bad. We should not demonize . . . the gatekeepers,” Brown said, adding that he believes aspects of the law enforcement structure “leaves those of a certain color at extreme risk.”

People should not just be hoping but “demanding for reform, knowing that something can be done and will be done,” Brown said.