Living on the streets nearly killed a homeless man in Brooklyn on Tuesday when he was mauled by an unleashed pit bull in an attack stopped only when an off-duty correction officer put a bullet in the animal’s head.

The attack occurred at about 1 p.m. on Herkimer Place near Perry Place in Bedford-Stuyvesant when the dog sank its teeth into the vagrant and wouldn’t let go, witnesses and cops said.

“He was bleeding a lot and screaming,” Anthony Davis, the officer who ran over when he heard the commotion, told The Post. “I tried to get him off the guy by pushing the dog and kicking him, but he wouldn’t let go. He had a tight lock on the guy’s chin and he wouldn’t let go.”

After trying to fight off the dog for about a minute, Davis — a 10-year Correction Department veteran who works at the Brooklyn House of Detention — blasted the female dog once in the head with his personal 9mm pistol, which he was legally carrying.

“I’ve had dogs since I was a kid — I had to make the decision to save that man’s life,” said Davis, 50. “I love dogs.”

But even after the fatal shot, it took several minutes for the dog to unclench its jaws from the man, witnesses said.

“There was so much blood,” said a witness who only identified himself as Alva, 59. “It’s a terrible situation. I’ve never seen anything like it.”

Norman Seabrook, president of the Correction Officers’ Benevolent Association, said the homelessness explosion must be addressed — especially in light of the attack.

“Unfortunately, there are far too many homeless individuals on the street and this dog could have attacked a vulnerable child or senior citizen,” Seabrook said.

The victim was taken to New York Methodist Hospital with bites to his arm and leg, officials said. Davis was treated for trauma at an area hospital.

A clerk at County Pharmacy on the corner of Herkimer Place identified the victim only as Bemo.

“He hangs out on the corner, smoking and drinking,” said employee Rafi Kahman, 21. “He’s not really a troublemaker. He’ll yell [at] you, but he’ll apologize.”

Witnesses said the dog is owned by another homeless man who often hangs out on Herkimer Place, but police could not confirm that.

That man refused to answer questions, telling a Post reporter: “Get the f–k out of my face.”

Additional reporting by Daniel Prendergast