A former US Marine killed 12 people inside a crowded California bar before taking his own life.

Ian David Long, a 28-year-old with mental health issues who served as a machine gunner in Afghanistan, carried out the shooting in Thousand Oaks about 40 miles north west of Los Angeles.

Investigators are still trying to find a motive for the attack at a venue hosting a country music evening for students.

The hooded killer used a smoke bomb and a handgun, sending hundreds of people fleeing in terror.

Ventura County Sheriff Geoff Dean said his department had several previous contacts with Long, including a call to his home in April, when deputies found him angry and acting irrationally.

The sheriff said a mental health crisis team was called at the time and concluded Long did not need to be taken into custody.

Patrons at the bar screamed in fear, shouted “Get down!” and used bar stools to smash second-floor windows and jump to safety as gunfire erupted at the Borderline Bar & Grill, a Thousand Oaks venue popular with students from nearby California Lutheran University.

The dead included 11 people inside the bar and a sheriff’s sergeant who was the first officer inside the door, the sheriff said.

“It’s a horrific scene in there,” Mr Dean said in a press conference at a car park.

“There’s blood everywhere.”

The killer deployed a smoke device and used a .45-calibre handgun.

California Bar Shooting
A victim is treated near the scene (AP)

It was the deadliest mass shooting in the US 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.

That, in turn, closely followed the series of pipe bombs mailed to critics of President Donald Trump.

Democratic governor-elect Gavin Newsom, in his first public appearance since winning office on Tuesday, lamented the violence that has come again to California.

“It’s a gun culture,” he said. “You can’t go to a bar or nightclub? You can’t go to church or synagogue? It’s insane is the only way to describe it. The normalisation, that’s the only way I can describe it. It’s become normalised.”

Mr Trump praised police for their “great bravery” in the California attack and said, “God bless all of the victims and families of the victims”.

California Bar Shooting
People comfort each other as they stand near the scene of the shooting (Mark J. Terrill/AP)

The gunman was tall and wearing all black with a hood and his face partly covered, witnesses told TV stations.

He first fired on a person working the door, then appeared to shoot at random at people inside, they said.

“I dropped to the floor,” Sarah Rose DeSon told ABC’s Good Morning America.

“A friend yelled, ‘Everybody down!’ We were hiding behind tables trying to keep ourselves covered.”

Sheriff’s Sergeant Ron Helus and a passing highway patrolman arrived at the Borderline around 11.20pm in response to several 911 calls, heard gunfire and went inside, the sheriff said.

Sgt Helus was immediately hit with multiple gunshots, Mr Dean said.

The highway patrolman pulled Sgt Helus out, then waited as a Swat team and scores more officers arrived.

Sgt Helus died early on Thursday at a hospital.

By the time they entered the bar again, the gunfire had stopped, according to the sheriff.

They found 12 people dead inside, including the gunman who apparently took his own life, 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 on Friday.

He praised the murdered officer, a close friend, as a hero, saying: “He went in there to save people and paid the ultimate price.”

Shootings of any kind are extremely rare in Thousand Oaks, a city of about 130,000 people about 40 miles west of Los Angeles, just across the county line.

The Borderline, which includes a large dance hall along with several smaller areas for eating and drinking, was holding its regular College Country Nights when the attack took place.

California Bar Shooting
Police at the scene (KABC/AP)

The bar is also close to several other universities, including California State University Channel Islands in Camarillo, Pepperdine University in Malibu and Moorpark College in Moorpark.

Nick Steinwender, Cal Lutheran student union president, told KTLA-TV he immediately started receiving messages about the shooting, and he and his roommate went to the scene to offer rides back to campus or moral support.

“It’s going to be a very sombre day,” Mr Steinwender said.

“I know we don’t have all the details in yet, but you know, it just feels like it’s an attack on our community.

“You know, I think it’s going to be something that we’re going to have to come together and move past.”

California Bar Shooting
An FBI agent (Mark J. Terrill/AP)

Cole Knapp, a freshman at Moorpark College, said he was inside the bar when the shooting began, but he thought at first that it was “just someone with an M-80, just kind of playing a prank”.

Then he said he saw the gunman, wearing a small black head covering and black hoodie and holding a handgun.

“I tried to get as many people to cover as I could,” Mr Knapp said. “There was an exit right next to me, so I went through that.

“That exit leads to a patio where people smoke. People out there didn’t really know what was going on.

California Bar Shooting
People walk away from the scene (Mark J. Terrill/AP)

“There’s a fence right there so I said, ‘Everyone get over the fence as quickly as you can’, and I followed them over.”

He said a highway patrol officer who happened to be pulling someone over was nearby.

“I screamed to him, ‘There’s a shooter in there!’ He was kind of in disbelief, then saw that I was serious,” Mr Knapp said.

He said he had friends who had not been accounted for.

Tayler Whitler, 19, said she was on the dance floor with her friends nearby when she saw the gunman shooting and heard screams of “Get down!”

California Bar Shooting
Sheriff’s deputies speak to potential witnesses (Mark J. Terrill/AP)

“It was really, really, really shocking,” Ms Whitler told KABC-TV as she stood with her father in the car park.

“It looked like he knew what he was doing.”

The dead sheriff’s officer was a 29-year veteran of the force with a wife and son and planned to retire in the coming year, said the sheriff, who choked back tears as he talked about the sergeant who was also his long-time friend.

“Ron was a hardworking, dedicated sheriff’s sergeant who was totally committed,” Dean said, “and tonight, as I told his wife, he died a hero because he went in to save lives.”