A 28-year-old Marine Corps veteran opened fire in a country music bar packed with college students in California overnight, killing 12 people including a police officer who exchanged shots with the gunman, authorities said Thursday.

Terrified patrons scrambled for the exits after the gunman started shooting a handgun in the Borderline Bar and Grill around 11:20 pm on Wednesday before killing himself, they said, AFP reported.

"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.

Dean identified the suspect as Ian David Long, a Marine Corps veteran and resident of Thousand Oaks, an upscale city northwest of Los Angeles.

According to the Pentagon, Long served in the Marine Corps from August 2008 to March 2013 and was a machine gunner.

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."

Long was found dead in an office at the bar by police 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."

The sheriff said Long drove to the bar and shot an unarmed security guard who was standing outside before opening fire at random inside.

Dean described the scene 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.

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

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.

Around 15 people were also injured, the sheriff said, including several who were hurt "jumping out of windows, diving under tables."

President Donald Trump tweeted that he had "been fully briefed on the terrible shooting," the latest chapter in America's epidemic of gun violence that came just 10 days after a gunman killed 11 worshippers at a synagogue in Pittsburgh.

Trump ordered the US flag at the White House and other government buildings to be flown at half-staff.

Last year, a country music festival in Las Vegas was the scene of the worst mass shooting in modern US history. A gunman shooting from the 32nd floor of a hotel and casino with high power weapons killed 58 people.