NEW YORK — Thousands of police officers lined the pews Friday at St. Patrick’s Cathedral to honor fallen Officer Jason Rivera, who was gunned down with his partner last week in an ambush that left the New York Police Department in mourning and the city on edge.

Mayor Eric Adams, himself a retired police captain, told those gathered that he saw an echo of himself in the slain officer who had joined a department he had seen as flawed in hopes of improving it.

“He did it for the right reasons — he wanted to make a difference,” said Adams, a Democrat who also sounded a message of support for a force that, like other police departments, has come under criticism during the last two years amid a national reckoning with policing, race and what public safety should mean.

“There were days when I felt the public did not understand and appreciate the job we were telling, and I want to tell you officers: They do. They do,” he said. “These two fine men watered the tree of safety that allows us to sit under the shade from the hot sun of violence.”

Rivera and a partner, Officer Wilbert Mora, were fatally wounded Jan. 21 by a gunman who ambushed them in a hallway as they responded to a family dispute. Mora’s funeral is set for next week, also at St. Patrick’s.

“The system continues to fail us. We are not safe anymore. Not even the members of the service,” Rivera’s wife, Dominique Luzuriaga, said through tears as she recalled her horror at seeing a cellphone alert about two officers being shot in Harlem and then worriedly texting and calling the former elementary school classmate she married just this past October.

Her messages went unanswered, until she got a call summoning her to a hospital where he was pronounced dead.

“I’m still in this nightmare that I wish I never had, full of rage and anger, hurt and sad, torn,” she said.

Both Mora and Rivera — who was posthumously promoted to detective on Friday — grew up in the city’s ethnic enclaves and had hoped to help the department build bridges with the community. Friends and fellow police officers describe them as caring and dedicated.

“Jason saw the need and had the desire to foster a positive relationship between police and his community,” said Inspector Amir Yakatally, the commanding officer of Rivera’s and Mora’s precinct. “He was what we all want in a cop.”

He noted that Rivera began a police career at a difficult time — amid the coronavirus pandemic and protests over policing and other issues — and was so excited to get to work that he double-parked in front of the stationhouse his first day and showed up early every workday after.

Jeffrey Rivera recalled that as a youngster, his brother — “Tata” to his family — listened to police radio transmissions, watched police dramas on TV and became “obsessed” with a law enforcement career.

Jason Rivera would eventually say, in an essay, that he had initially been angered by policing — particularly being pulled over in a taxi and seeing officers frisk his brother — but came to feel that the NYPD was trying to do better at community relations.

Cardinal Timothy Dolan presided over Rivera’s service, held in Spanish and English in recognition of the first-generation New Yorker’s Dominican heritage.

Officers came from near and far to honor Rivera, 22, who was recently married to his childhood sweetheart and barely into his second year of service on the force.

“It doesn’t really matter what uniform we wear,” said Master Police Officer Tammy Russel, from Fairfax County, Virginia. “These are all our brothers and sisters. … Sadness all around.”

The gunman, who was shot by a third officer, died earlier in the week. Authorities are still investigating why he fired at the officers.

New York Gov. Kathy Hochul said flags at state buildings would fly at half-staff from sunrise the day of Rivera’s funeral until sunset the day of Mora’s funeral on Wednesday.

Before Friday, the last NYPD officer killed in the line of duty was Anastasios Tsakos, who was struck by a suspected drunken driver in May 2021 while assisting officers at the scene of an earlier crash on a Queens highway.

The last NYPD officer fatally shot in the line of duty, Brian Mulkeen, was hit by friendly fire while struggling with an armed man after chasing and shooting at him in the Bronx in September 2019.

Two officers, Randolph Holder and Brian Moore, were killed in separate shootings in 2015. The year before, Officers Wenjian Liu and Rafael Ramos were fatally shot by a man who ambushed them as they sat in their patrol car in Brooklyn.