Authorities said the motive for the attack Wednesday night was under investigation.
The killer was identified as 28-year-old Ian David Long, a former machine gunner and veteran of the war in Afghanistan who was interviewed by police at his home last spring after an episode of agitated behavior that they were told might be post-traumatic stress disorder.
Screaming in fear, patrons rushed for the exits, dived under tables and used barstools to smash second-floor windows and jump to safety as gunfire reverberated through the Borderline Bar & Grill, a hangout popular with students from nearby California Lutheran University.
"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."
The dead included 11 people inside the bar and a veteran sheriff's sergeant who was the first officer inside the door, Ventura County Sheriff Geoff Dean said.
"It's a horrific scene in there," Dean said in the parking lot. "There's blood everywhere."
The bloodshed was the latest in what seems to be a never-ending string of mass shootings that are happening with terrifying frequency across the United States.
All morning, people looking for missing friends and relatives arrived at a community center where authorities and counselors were informing the next-of-kin of those who died. Many walked past TV cameras with blank stares or tears in their eyes. In the parking lot, people comforted each other with hugs or a pat on the back.
Jason Coffman received the news that his son Cody, 22, who was about to join the Army, was dead. 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.
"Oh, Cody, I love you, son," Coffman sobbed.
Thousand Oaks mass shooting: Father identifies son as one of 12 slain in massacre in heartbreaking press conference
"Sister Sister" actress Tamera Mowry-Housley and her husband said their 18-year-old niece Alaina Housley, a student at nearby Pepperdine University, was also among those killed . So was Justin Meek, a 23-year-old recent graduate of Cal Lutheran, according to the university.
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.
Thousand Oaks shooting: Actress Tamera Mowry's niece Alaina Housley among dead
Democratic Gov.-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 normalization, that's the only way I can describe it. It's become normalized."
President Donald Trump praised police for their "great bravery" in the attack and ordered flags flown at half-staff in honor of the victims.
I have been fully briefed on the terrible shooting in California. Law Enforcement and First Responders, together with the FBI, are on scene. 13 people, at this time, have been reported dead. Likewise, the shooter is dead, along with the first police officer to enter the bar....— Donald J. Trump (@realDonaldTrump) November 8, 2018
Long was armed with a Glock 21, a .45-caliber pistol designed to hold 10 rounds plus one in the chamber, according to the sheriff. But it had an extended magazine - one capable of holding more ammunition - that is illegal in California, Dean said.
The killer also deployed a smoke device, a law enforcement official told The Associated Press. The official was not authorized to discuss the investigation publicly and spoke on condition of anonymity.
Authorities converged on Long's home in Newbury Park, about five miles from the Borderline bar, in a search for clues to what set him off.
"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 honorably discharged, the military said. Court records show he married in 2009 and was divorced in 2013.
Authorities said he had no criminal record, but in April officers were called to his home, where deputies found him angry and acting irrationally. The sheriff said officers were told he might have PTSD because of his military service. A mental health specialist met with him and didn't feel he needed to be hospitalized.
EMERGENCY CALLS: Law enforcement dispatch calls during mass shooting
Tom Hanson, 70, who lives next door to Long and his mother, said Thursday that he called the police about six months ago when he heard "heavy-duty banging" and shouting coming from the Long's home.
"I was concerned because I knew he had been in the military," he said.
Hanson said the sheriff's deputy who arrived took his information, but he never learned more about what happened and hadn't spoken to Long since then. He said he was "dumbfounded" by the bloodshed.
The gunman first shot a security guard standing outside, then went in and opened fire on staff members and patrons, the sheriff said.
Sheriff's Sgt. Ron Helus and a passing highway patrolman arrived around 11:20 p.m. in response to several 911 calls, heard gunfire and went inside, the sheriff said. Helus was immediately shot, Dean said.
The highway patrolman pulled Helus out, then waited as a SWAT team and scores more officers arrived. Helus died at a hospital.
Thousand Oaks shooting: Sheriff Sergeant Ron Helus was 'hard working, dedicated' and 'died a hero'
By the time officers entered the bar again, the gunfire had stopped, according to the sheriff. They found 12 people dead inside, including the gunman, who was discovered in an office and had apparently shot himself, the sheriff said.
"There's no doubt that they saved lives by going in there and engaging with the suspect," said Dean, who was set to retire on Friday. He praised the slain officer - a close friend - as a hero: "He went in there to save people and paid the ultimate price."
One other person was wounded by gunfire, and as many as 15 others suffered minor injuries from jumping out windows or diving under tables, authorities said.
People at the bar fled from all exits, broke through windows and hid in the attic and bathrooms, the sheriff said. He said they seemed to know what to do.
"Unfortunately our young people, people at nightclubs, have learned that this may happen, and they think about that. Fortunately it helped save a lot of lives that they fled the scene so rapidly," he said.
Shootings of any kind are extremely rare in Thousand Oaks, a city of about 130,000 people about 40 miles (64 kilometers) from Los Angeles, just across the county line.
The Borderline, which has a large dance hall along with several smaller areas for eating and drinking, was holding one of its regular "College Country Nights."
Nick Steinwender, Cal Lutheran student body 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 somber day," 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."
The bar is also close to several other universities, including California State University Channel Islands in Camarillo, Pepperdine in Malibu and Moorpark College in Moorpark.
Cole Knapp, a freshman at Moorpark, said he was inside the bar when the shooting began, but 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.
"I tried to get as many people to cover as I could," 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. 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 he had friends who were unaccounted for.
Tayler Whitler, 19, said she was on the dance floor when she saw the gunman shooting and heard screams of "Get down!"
"It looked like he knew what he was doing," Whitler told KABC-TV.
Around midday, the body of the slain sheriff's officer was taken by motorcade from the hospital to the coroner's office. Thousands of people stood along the route or pulled over in their vehicles to watch the hearse pass. Firefighters use two ladder trucks to raise a giant American flag over the route.
Helus was a 29-year veteran of the force with a wife and son and planned to retire in the coming year, said the sheriff, choking back tears.
In Harris County, Sheriff Ed Gonzales took to Twitter to share his condolences for those affected, especially the Ventura County Sheriff's deputy.
Sheriff Sergeant Ron Helus was 'hard working, dedicated' and 'died a hero'
Hero saves as many as 30 people during Thousand Oaks shooting
What we know about Thousand Oaks shooting suspect
WITNESSES: Stepdad and son recall terrifying moments gunfire erupted
PHOTOS: Deadly shooting in Thousand Oaks nightclub