BUFFALO, Minn. — The man accused of shooting five people at a rural Minnesota health clinic also detonated three explosive devices inside the facility as he opened fire on medical staff and patients, authorities said Thursday, amid deepening questions over whether the suspect should have been more closely monitored by law enforcement agencies before his deadly rampage.

Gregory Paul Ulrich, 67, was formally charged Thursday with one count of second-degree murder, four counts of attempted murder, and one count each of carrying a pistol without a permit and “gross disregard for life and property” by utilizing an explosive device. Bail was set at $5 million.

“The behavior of Mr. Ulrich implicated the safety of the public in the highest regard,” Wright County Attorney Brian Lutes said during a brief court hearing, conducted over Zoom. “He went to that clinic knowing he was going to shoot that clinic up. He went to that clinic knowing he was going to ignite those bombs, and that is just what he did.”

Ulrich, who reportedly was battling an opioid addiction and was upset that a doctor who worked at the clinic had stopped issuing him prescriptions for the drug, did not speak during the hearing. His public defender declined to file a formal plea in the case, pending another hearing next month.

The shooting appears to be the latest in a long line of violent public attacks that occurred after ominous warnings and red flags by those later accused of carrying them out. But authorities struggled Thursday to answer questions about how Ulrich obtained the gun allegedly used during the attack and whether police should have known sooner that he could pose a threat to the clinic’s staff.

According to charging documents, Ulrich stormed the reception area of Buffalo’s Allina Medical Clinic, about 40 miles northwest of Minneapolis, on Tuesday and shot five people, one of them fatally, with a 9mm pistol. Ulrich, who authorities and records show had a history of making threats against clinic staff, had packed black gunpowder into cylinder containers to construct at least four explosive devices.

Authorities say Ulrich detonated two of the devices in the clinic’s lobby and another in a nearby hallway, causing explosions that ripped a hole in a metal door and shattered windows. A fourth unexploded device was found nearby, and authorities later discovered a pound of gunpowder at his previous residence.

Speaking at a news conference, Lutes said Ulrich did not have an extensive criminal record, although he was prosecuted on several charges of driving while intoxicated from 2004 to 2006.

Lutes also confirmed that a doctor who worked at the Allina Clinic filed a restraining order against Ulrich in 2018 because of threats.

Within a few months, Ulrich faced a misdemeanor charge of violating that order, which was handled by local prosecutors in Buffalo, Lutes said. But before that case could be heard, Ulrich was determined to be “incompetent,” and under Minnesota law misdemeanors are to be dismissed under those circumstances, Lutes said.

Ulrich’s brother, Richard, told The Washington Post earlier this week that Gregory Ulrich had worked in construction and became addicted to opioids a few years ago after he had back surgery for an old injury he had suffered on the job. He said his brother was angry and upset that doctors had stopped giving him prescriptions for the medication.

Still, a man who used to live with Ulrich told The Post that Ulrich was still able to obtain a permit to buy a Smith & Wesson handgun.

In Minnesota, permits to carry handguns are issued by county sheriffs or local police departments. The Minnesota Bureau of Criminal Apprehension says on its website that such permits can be denied by a sheriff “if there is a substantial likelihood that the applicant is a danger to himself/herself or others if issued a permit to carry.” The five-page application to purchase or carry guns include things that would disqualify a person from obtaining a permit, including being convicted of domestic violence or judicially committed to a mental health or drug treatment facility.

The weapons charge against Ulrich suggests he may not have had a permit to carry a gun in public.

But Lutes and Wright County Sheriff Sean Deringer both declined to discuss whether Ulrich had legally obtained a permit to buy his firearm, citing Minnesota confidentiality laws involving the possession or transfer of firearms.

Lutes said that his office was working to get a court order signed by a judge to release those records, but the order was denied.

“We cannot make comments on that,” Lutes said. ‘The Minnesota government data is very private. This is private, not public data. And so we have to be very careful about what we release.”

But in response to a reporter's question at a news conference, Deringer said it is not uncommon for Minnesota law enforcement agencies to mistakenly issue some permits, which he said can occur because of “human error.”

“The flags are there in the systems that we have,” Deringer said. “And that system is overlooked, or the flag is overlooked. The other thing is sometimes, and I’ve said this before, is you have to have a master's degree to read some of these criminal histories. They’re not always clear . . . So sometimes they do slip through the cracks.”

As related directly to Ulrich’s alleged behaviors, Deringer said his office was aware that he has made “previous threats” but added none of those threats were made “in the past several months or even a year.”

“Or we would have taken immediate action to try to circumvent or prevent what happened Tuesday,” Deringer said. “If we are going to push blame, I ask people to push blame where blame is due, and that is on a suspect who decided to go into a Buffalo clinic and victimize people who are truly trying to help their communities.”

Research has found that armed attackers who open fire in public places have often worried people around them and left red flags that littered their paths to violence. An FBI study of active shooters, released in 2018, found that attackers had behaved in concerning ways noticed by others beforehand.

These attackers did not just snap one day, the study’s authors said. Most were fueled by “a grievance of some kind,” the study found, often some sort of action taken against them beforehand. Even if these grievances were not “reasonable or even grounded in reality,” the study said, they still helped give these attackers a sense of purpose.

The study examined dozens of attackers who opened fire in public places between 2000 and 2013. Most of them, the study noted, had obtained their guns legally.

“The notion that somebody impulsively walks down the street and randomly selects a facility is just not true,” said Matthew Doherty, who used to run the Secret Service’s Threat Assessment Center and is now a security consultant with Jensen Hughes. “They’re planned in advance.”

In Parkland, Fla., where 17 people were killed at a high school in 2018, law enforcement officials acknowledged later that they had received repeated warnings about the alleged shooter. After a gunman killed five people in Aurora, Ill., in 2019, prosecutors said that the day of the shooting, the attacker had told a co-worker he would kill people if he got fired hours before that scenario unfolded.

Some states have adopted “red flag” laws that can allow relatives or law enforcement officials to petition courts to restrict a person’s access to guns. Minnesota does not have such a law, according to the Giffords Law Center.

The Minnesota shooting is yet another reminder that law enforcement officials need to “conduct threat management, as opposed to conducting a criminal investigation looking for a violation of the law,” Doherty said.

A lot of people might make threats, Doherty said, but what’s important is trying to determine whether they actually pose a threat. That includes examining their communications about possible plans, looking at attempts to purchase weapons and speaking to friends, family, neighbors or others who might have interacted with the person, he said.

“Obviously there were some red flags,” Doherty said of the Buffalo shooting. “But the key is, were there management techniques employed? And at this point, I highly doubt it.”

Minnesota has a strong justice system, but “it’s a look-back system,” said Joseph Tamburino, a criminal defense attorney in Minneapolis and a former public defender. “It’s very difficult to be proactive. So yes, anytime you get a case like Mr. Ulrich’s you can say, well, looking back, things should’ve happened. However, it’s really difficult to say this was missed in some way.”

Berman and Craig reported from Washington.