LOUISVILLE, Ky. — 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 percent 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 percent 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."

Rep. Fred Keller, R-Snyder, from right, speaks as President Donald Trump looks on during a campaign rally in Montoursville, Pa., on May 20, 2019. Matt Rourke / AP

In a Pennsylvania special election, conservative state lawmaker Fred Keller won the special election for Congress in a heavily Republican district that sprawls across areas of central and northern Pennsylvania that are a stronghold for Trump.

Keller will replace Republican Tom Marino, who resigned in January after he emerged as a strong Trump ally in Congress.

Keller, a fifth-term state representative, beat 13 other candidates seeking the GOP nomination in the heavily Republican district before besting Marc Friedenberg on Tuesday.

Friedenberg, a lawyer and Penn State information technology instructor, was the only Democrat to seek his party's nomination.

The current term ends in January 2021. The 12th District covers all or parts of 15 counties and is a strong source of support for Trump. Voters there backed the Republican in 2016's presidential election by better than 2 to 1 over Democrat Hillary Clinton.

Before he ran for the Legislature, Keller was a plant manager for a wood cabinet supplier and ran a real estate management business.