Kentucky Attorney General and Democratic gubernatorial candidate Andy Beshear, right, and his running mate Jacqueline Coleman wave to their supporters following their victory speech in Louisville, Ky., Tuesday, May 21, 2019. (AP Photo/Timothy D. Easley)

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.

Kentucky Attorney General Andy Beshear speaks to his supporters following his victory in the democratic primary for Governor in Louisville, Ky., Tuesday, May 21, 2019. (AP Photo/Timothy D. Easley)
Kentucky Gov. Matt Bevin addresses the media after winning the Republican gubernatorial primary, in Frankfort, Ky., Tuesday, May 21, 2019. At right is Kentucky Sen. Ralph Alvarado, the Republican nominee for lieutenant governor. (AP Photo/Bryan Woolston)
FILE - In this March 26, 2019, file photo, Kentucky Gov. Matt Bevin speaks with the media during an event about the new Interstate 165 in Bowling Green, Ky. Kentucky Republicans will give an initial verdict on Bevin’s job performance in the state's primary election Tuesday, May 21. Meanwhile, Democrats will choose from three prominent candidates looking to challenge Bevin, an ally of President Donald Trump.  (Bac Totrong/Daily News via AP, File)
Kentucky attorney general and Democratic gubernatorial candidate Andy Beshear talks with reporters during a campaign stop at Spencer's Coffee, Friday, May 17, 2019 in Bowling Green, Ky. (Bac Totrong/Daily News via AP)
FILE - In a Tuesday, Jan. 8, 2019 file photo, Kentucky state Rep. Robert Goforth, R-East Bernstadt, left, speaks with running mate Lawrence County Attorney Mike Hogan after officially filing for governor at the Kentucky State Capitol in Frankfort, Ky. 
Undaunted by his status as a political underdog, state Goforth has logged more than 10,000 miles in his pickup truck and invested at least $750,000 of his own money into challenging Kentucky’s governor in the state’s Republican primary. (AP Photo/Timothy D. Easley, File)
FILE - In this Wednesday, April 24, 2019, file photo, former state Auditor Adam Edelen, a Democratic candidate for Kentucky governor, responds during a debate at Transylvania University in Lexington, Ky. A well-funded group backing Edelen shook up Kentucky's Democratic gubernatorial primary on Thursday, May 16, running a short-lived TV ad denouncing rival Andy Beshear's private legal work for the Boy Scouts of America in a sexual abuse case years ago. (AP Photo/Timothy D. Easley, File)
In this Wednesday, April 24, 2019 photo, Democratic candidate for Kentucky governor, state Rep. Rocky Adkins, D-Sandy Hook, responds during a debate at Transylvania University in Lexington, Ky. The three leading Kentucky Democrats on the May primary ballot agree their state badly needs a new chief executive. (AP Photo/Timothy D. Easley)
Kentucky attorney general and Democratic gubernatorial candidate Andy Beshear talks with reporters during a campaign stop at Spencer's Coffee, Friday, May 17, 2019 in Bowling Green, Ky. (Bac Totrong/Daily News via AP)
Minority floor leader and gubernatorial candidate Rep. Rocky Adkins. center, D-Sandy Hook,  talks with people Wednesday, May 1, 2019, at the Bowling Green Rotary Club in Bowling Green, Ky.  (Bac Totrong/Daily News via AP)
FILE - In this Wednesday, April 24, 2019, file photo, candidates for Kentucky governor, from left, Democratic former state Auditor Adam Edelen, Kentucky state Rep. Rocky Adkins, D-Sandy Hook, and Democratic Attorney General Andy Beshear stand on stage during a debate at Transylvania University in Lexington, Ky. The three leading Kentucky Democrats on the May primary ballot for governor agree their state badly needs a new chief executive. (AP Photo/Timothy D. Easley, File)
Kentucky Gov. Matt Bevin and Kentucky Senator Ralph Alvarado, the republican nominee for lieutenant governor, speak to the media after winning the republican gubernatorial primary, in Frankfort, Ky., Tuesday, May 21, 2019. (AP Photo/Bryan Woolston)
Kentucky Gov. Matt Bevin and Kentucky Senator Ralph Alvarado, the republican nominee for lieutenant governor, speak to the media after winning the republican gubernatorial primary, in Frankfort, Ky., Tuesday, May 21, 2019. (AP Photo/Bryan Woolston)
Kentucky Gov. Matt Bevin speaks to the media after winning the republican gubernatorial primary, in Frankfort, Ky., Tuesday, May 21, 2019. (AP Photo/Bryan Woolston)
Kentucky Gov. Matt Bevin, right, with Kentucky Senator Ralph Alvarado, the republican nominee for lieutenant governor, listens as he speaks to the media after winning the republican gubernatorial primary, in Frankfort, Ky., Tuesday, May 21, 2019. (AP Photo/Bryan Woolston)
Kentucky Gov. Matt Bevin, left, with Kentucky Sen. Ralph Alvarado, the republican nominee for lieutenant governor, speaks to the media after winning the republican gubernatorial primary, in Frankfort, Ky., Tuesday, May 21, 2019. (AP Photo/Bryan Woolston)
Kentucky Gov. Matt Bevin, with Kentucky Senator Ralph Alvarado, the republican nominee for lieutenant governor, speaks to the media after winning the republican gubernatorial primary, in Frankfort, Ky., Tuesday, May 21, 2019. (AP Photo/Bryan Woolston)