Firefighters salute as a motorcade passes by transporting the body of Sergeant Ron Helus, killed in a shooting at a country bar in Thousand Oaks, California

Thousand Oaks (United States) (AFP) - A 28-year-old US Marine Corps combat veteran opened fire in a crowded country music bar in California, killing 12 people including a police officer who rushed in and exchanged shots with the gunman, authorities said Thursday.

Terrified patrons – many of whom were college students – scrambled for the exits after the gunman started shooting people with a handgun in the Borderline Bar and Grill at around 11:20 pm on Wednesday.

“They ran out of the back doors, they broke windows, they went through windows. They hid up in the attic, they hid in the bathroom,” Ventura County Sheriff Geoff Dean said.

The assailant, identified as Ian David Long, a troubled former machine gunner who served a tour in Afghanistan, died of a self-inflicted gunshot wound.

A police car outside a country music bar in Thousand Oaks, California, where a gunman shot dead 12 people before killing himself

Dean told reporters that Long shot an unarmed security guard who was standing outside the bar before entering and opening fire at random inside.

Twenty-three people were injured – some from “jumping out of windows, diving under tables,” Dean said – and treated at area hospitals.

A year ago, 58 people were shot dead at a country music festival in Las Vegas, the worst mass shooting in modern US history, and several of the patrons in the California bar had also been at the Nevada concert.

“I can’t believe I’m saying this again,” Molly Maurer posted on Facebook. “I’m alive and I’m home safe.

Friends hug outside Los Robles Medical Center in Thousands Oaks, California, paying tribute to a police officer killed at the Borderline Bar and Grill

According to the Pentagon, Long served in the Marine Corps from August 2008 to March 2013.

He attained the rank of corporal and saw combat while deployed in Afghanistan from November 2010 to June 2011.

“He had perfect form,” bar patron Teylor Whittler told Fox News. “He looked like he knew what he was doing.”

- ‘Like hell’ -

Police on Thursday were searching Long’s house in Newbury Park, near Thousand Oaks, where he reportedly lived with his mother.

Police and FBI agents outside the home of Ian David Long, identified as the gunman

Long was found dead by police in an office at the bar and is believed to have shot himself, Dean said.

Authorities so far had no indication of a motive, or of any connection to terrorism.

“We have no idea what the motive was at this time,” Dean said.

“Obviously he had something going on in his head that caused him to do something like this,” he said. “He had some kind of issues.”

Dean described the scene inside the bar as “like hell.”

He said his department had “several contacts” with Long over the years, for minor incidents including a traffic collision, and in 2015 when he was beaten up at a local bar.

A witness talks to reporters outside the bar, which was packed with students on a "college night"

In April this year, deputies were called to his house for a disturbance and found him “acting a little irrationally.”

“They felt he might be suffering from PTSD, the fact he was a veteran and had been in the corps,” Dean said.

“They called out our crisis intervention team, our mental health specialists who met with him, talked to him, and cleared him.”

Dean said Long was armed with a single handgun, a .45 caliber Glock 21, which he purchased legally.

- ‘Ultimate sacrifice’ -

Among those killed was a three-decade veteran of the sheriff’s department, 54-year-old Sergeant Ron Helus, who was married, had a grown son and was close to retirement.

Map of California locating shooting at a bar in Thousand Oaks

“He went in there to save people and made the ultimate sacrifice,” Dean said.

He said Helus was on the phone with his wife when he got the report of the shooting and he told her, “‘Hey, I have to go. I love you. I’ll talk to you later.’”

Helus and a highway patrol officer exchanged gunfire with Long, he said, and the sergeant was shot “several times.”

Matt Wennerstrom, a 20-year-old college student, said the shooter apparently had a 10-15 round magazine on his gun.

“When (he) started to reload, that’s when we got people out of there and I didn’t look back,” Wennerstrom said.

He said he and others smashed their way out of the bar onto a balcony and then jumped down to safety.

Jasmin Alexander, who was with around 15 friends at the bar, said there was chaos and confusion inside when shots rang out.

“We were just at the bar, having fun, dancing,” she told reporters.

“All of a sudden we heard the ‘bang, bang’ of the gunshots and it just started going crazy and people were pushing.

“We didn’t take it seriously at first… because it sounded like firecrackers and everyone just dropped down to the floor.”

President Donald Trump ordered the US flag at the White House and other government buildings to be flown at half-staff following the shooting, which came just 10 days after a gunman killed 11 worshippers at a synagogue in Pittsburgh.