Terrified patrons hurled barstools through windows to escape or threw their bodies protectively on top of friends as a Marine combat veteran killed 12 people at a country music bar in southern California.

Dressed all in black with his hood pulled up, the gunman apparently took his own life as scores of police converged on the Borderline Bar and Grill in Thousand Oaks, about 40 miles north-west of Los Angeles.

The motive for the rampage late on Wednesday night was under investigation.

California Bar Shooting
People comfort each other as they sit near the scene in Thousand Oaks (Mark J Terrill/AP)

The killer, Ian David Long, 28, was a former machine gunner and Afghanistan war veteran who was interviewed by police at his home last spring after an episode of agitated behaviour that authorities were told might be post-traumatic stress disorder.

California Bar Shooting
Authorities said Marine combat veteran Ian David Long opened fire at a country music bar in southern California (California Department of Motor Vehicles via AP)

Opening fire with a handgun with an illegal, extra-capacity magazine, Long shot a security guard outside the bar and then went in and took aim at employees and customers, authorities said. He also used a smoke bomb, according to a law enforcement official.

The dead included a man who had survived last year’s massacre in Las Vegas, a veteran sheriff’s deputy who rushed in to confront the gunman, a 22-year-old man who planned to join the Army, a freshman at nearby Pepperdine University and a recent Cal Lutheran graduate.

“It’s a horrific scene in there,” Ventura County Sheriff Geoff Dean said in the parking lot. “There’s blood everywhere.”

Survivors of the rampage — mostly young people who had gone out for college night at the Borderline, a hangout popular with students from nearby California Lutheran University and other schools — seemed to know what to do, having come of age in an era of active-shooter drills and deadly rampages happening with terrifying frequency.

For some it was not a new experience. Survivors and their relatives said several people who were at the bar had been at the outdoor country music festival in Las Vegas last year where a gunman in a high-rise hotel killed 58 people.

“I don’t want prayers. I don’t want thoughts,” said Susan Schmidt-Orfanos, whose son Telemachus Orfanos survived the Vegas shooting only to die less than 10 minutes from his home.

“I want those… in Congress — they need to pass gun control so no-one else has a child that doesn’t come home.”

Many of the estimated 150 patrons at the Borderline dived under tables, ran for exits, broke through windows or hid in the attic and bathrooms, authorities and witnesses said.

“Unfortunately our young people, people at nightclubs, have learned that this may happen, and they think about that,” the sheriff said. “Fortunately it helped save a lot of lives that they fled the scene so rapidly.”

California Bar Shooting
Flower near the scene of the mass shooting in Thousand Oaks (Ringo HW Chiu/AP)

Matt Wennerstrom said he instinctively pulled people behind a pool table, and he and friends shielded women with their bodies after hearing the shots.

When the gunman paused to reload, Mr Wennerstrom said, he and others shattered windows with barstools and helped about 30 people escape. He heard another volley of shots once he was safely outside.

“All I wanted to do was get as many people out of there as possible,” he told KABC-TV. “I know where I’m going if I die, so I was not worried.”

Jason Coffman received the news that his son Cody, 22, who was about to join the Army, was dead.

Mr Coffman broke down as he told reporters how his last words to his son as he went out that night were not to drink and drive and that he loved him.

It was the nation’s deadliest such attack since 17 students and teachers were killed at a Parkland, Florida, high school nine months ago. It also came less than two weeks after a gunman massacred 11 people at a synagogue in Pittsburgh.

President Donald Trump praised police for their “great bravery” in the attack and ordered flags flown at half-staff in honour of the victims.

Authorities searched Long’s home in Newbury Park, about five miles from the Borderline bar.

“There’s no indication that he targeted the employees. We haven’t found any correlation,” the sheriff said. “Maybe there was a motive for this particular night, but we have no information leading to that at all.”

Long was in the Marines from 2008 to 2013, rose to the rank of corporal and served in Afghanistan in 2010-11 before he was honourably discharged, the military said. Court records show he married in 2009 and was divorced in 2013.

Sheriff’s Sargeant Ron Helus and a passing highway patrolman arrived at the club around 11.pm local time. in response to several 911 calls, heard gunfire and went inside, the sheriff said.

Sgt Helus was shot immediately, Mr Dean said.

The highway patrolman pulled Sgt Helus out, then waited as a Swat team and other officers arrived. Sgt Helus died at a hospital.

By the time officers entered the bar again — about 15 to 20 minutes later, according to the sheriff’s office — the gunfire had stopped. They found 12 people dead inside, including the gunman, who was discovered in an office, the sheriff said.

“There’s no doubt that they saved lives by going in there and engaging with the suspect,” said Mr Dean, who was set to retire Friday. He praised the killed officer — a close friend — as a hero: “He went in there to save people and paid the ultimate price.”