LOUISVILLE, Ky. (AP) — Kentucky Republican Gov. Matt Bevin cleared his first hurdle toward a second term but had to fend off a strong primary challenge Tuesday, setting up a long-anticipated showdown with his arch-nemesis — Democrat Andy Beshear — that will settle the feud they've fought in courtrooms over education and pension policies.

While Bevin claimed the nomination in GOP-leaning Kentucky, an upstart challenger — state Rep. Robert Goforth — attracted nearly 40% of the vote in a sign the combative incumbent has fence mending to do with his political base after his high-profile feuds with public school teachers.

Bevin got a last-minute boost from President Donald Trump, a key ally who looms as a huge asset as the governor tries to overcome self-inflicted damage in what will be a grudge match against Beshear. National political experts will be looking to see whether a Republican incumbent closely aligned with the president might be more vulnerable than expected.

Beshear, the state's attorney general, defeated two prominent rivals — Rocky Adkins and Adam Edelen — in the four-candidate Democratic primary. He'll try to restore the governorship for Democrats and carry on a family tradition. His father, Steve, was a popular governor whose two terms preceded Bevin's tenure.

Now he's on to the main event to be settled in November — Bevin vs. Beshear.

Bevin immediately tried to frame the matchup Tuesday night, saying: "It's going to be a remarkably stark contrast between the two tickets — conservative vs. liberal, black and white, night and day."

Beshear ripped into Bevin's policies on health care, pensions and education in his own preview of the fall campaign.

"It is not about what's going on in Washington, D.C.," Beshear said in declaring victory. "And it's not about right vs. left. Folks, it's about right vs. wrong."

Wielding his authority as the state's top lawyer, Beshear emerged as a Democratic obstacle to Republican dominance of state government. He challenged several of Bevin's executive actions and sued to block Bevin-backed pension and education initiatives in high-profile lawsuits. Beshear filed the suit that led Kentucky's Supreme Court to strike down a Bevin-backed pension law on procedural grounds last year. The pension measure sparked massive protests by teachers who converged on the state Capitol.

"Suing me is not beating me," Bevin said on Tuesday night.

Bevin has the advantage of heading the Republican ticket in a state that has trended overwhelmingly toward the GOP in recent elections. In his low-profile primary campaign, Bevin touted job growth, low unemployment and his alliance with Trump, who overwhelmingly won Kentucky in 2016 and remains a political force in the bluegrass state.

Trump waded into the GOP primary by tweeting his support for Bevin and recording a phone message urging Republican voters to back the governor. Bevin shares a style similar to Trump's. The Republican businessmen are proudly unconventional conservatives who favor social media and attack critics fiercely.

But Bevin's most prominent Republican challenger garnered significant support. Goforth put at least $750,000 of his own money into his insurgent campaign, which attacked Bevin for his combative style and his struggles on the pension issue.

With 100% of precincts reporting, Bevin had 136,060 votes, or 52 percent, while Goforth had 101,343 votes, or 39 percent. Two other challengers drew the rest of the votes.

Goforth's performance shows Bevin has "work to do," GOP strategist Scott Jennings said, predicting that the Bevin-Beshear showdown will be close.

"I'm sure Trump will help shore up Bevin's GOP flank," Jennings said. "And that's his imperative now: nationalize this race."

It's a formula that's been effective for the GOP in becoming the state's dominant party.

In a pre-emptive shot, the Democratic Governors Association said even Bevin's allies at the White House are worried about his reelection.

"They have every right to be worried — the bluegrass state is ready to turn the page from the failed Bevin era," the group said in a statement.

During the primary campaign, Bevin was a lightning-rod target for the Democrats. Goforth also ran an aggressive campaign, trying to capitalize on the governor's public spats with teachers.

Bevin has sharply criticized teachers who used sick days to rally at Kentucky's Capitol, forcing some school districts to close.

In 2018, he asserted without evidence that a child who had been left home alone was sexually assaulted on a day of mass school closings as Kentucky teachers rallied. He apologized but then doubled down last month, connecting a young girl's shooting in Louisville with school closings caused by more teacher protests.

Beshear continued his portrayal of Bevin as a bully in setting the stage for their coming showdown.

"We were raised better than this," he told supporters Tuesday night.

Kentucky teachers rallied last year to oppose pension changes and to demand more funding for schools. Protests continued this year against some education measures. The demonstrations were part of a nationwide wave of teacher activism.

Bevin has steered Kentucky on a conservative course along with the state's GOP-dominated legislature. He supports school choice efforts and signed measures to restrict abortion, allow people to carry a concealed handgun without a permit or training and a right-to-work measure letting workers evade union fees. Several of the abortion measures are being challenged in court.

He's also tried to revamp the state's Medicaid program to require "able-bodied" adults to get a job, go to school or volunteer as a condition to continue receiving the benefits. A federal judge blocked the rules and Bevin's administration appealed. Beshear has condemned Bevin's efforts, which would scale back one of his father's biggest achievements.

As governor, Steve Beshear expanded Kentucky's Medicaid program to include coverage for able-bodied adults, increasing the rolls by more than 400,000 people in a state with chronic health problems. It was an option given to states by former President Barack Obama's signature health-care law.

Copyright 2019 The Associated Press. All rights reserved. This material may not be published, broadcast, rewritten or redistributed.