FALLS CHURCH, Va. — Progressive Democrats were unable Tuesday night to nominate their preferred anti-Trump candidate, Tom Perriello, into the general election in Virginia’s gubernatorial election, but the party’s enthusiasm to repudiate President Trump was still clearly demonstrated by the record number of voters who helped nominate Lt. Gov. Ralph Northam instead.

Republican primary voters, meanwhile, came close to nominating Corey Stewart, a candidate who took Trumpism to extremes, embracing the Confederate flag as an emblem of his campaign. Establishment GOP choice Ed Gillespie was declared the winner by little more than 4,000 votes.

Northam, the overwhelming choice of the state’s Democratic establishment, turned back a challenge from Perriello, winning by a strong margin of 56 percent to 44 percent.

Northam, the 57-year-old lieutenant governor of Virginia, was the clear choice of the Democratic establishment in the state. Perriello, 42, jumped into the race unexpectedly in January, in response to Trump winning the presidency. If Gillespie’s lead holds, Northam will face off with him in the general election on Nov. 7.

Gillespie, 55, a former Republican National Committee chairman and White House adviser to President George W. Bush, had been expected to soundly defeat two other candidates, state Sen. Frank Wagner and Corey Stewart, the chairman of the Prince William County board of supervisors. Gillespie led by 20 points in one of the most recent polls.

But Stewart, 48, ran much closer than most expected after he’d run a campaign in which his defense of the Confederate flag inspired a supporter to fly a plane pulling the flag behind it over a political event, and in which he argued that Virginia’s “identity” was at risk if Confederate statues and monuments were removed or put in museums rather than in public spaces.

Democrats flocked to the polls to elect Northam, with more than 545,000 total voters swamping the turnout totals from the last contested Democratic gubernatorial primary in 2009, when 319,000 Democrats voted. By comparison, roughly 365,000 Republicans cast votes in the primary this year.

Perriello ran as a more populist candidate than Northam, and argued that Virginia Democrats needed more of a fighter to represent them in a race that will draw national attention this fall, serving as a referendum on the Trump presidency. He was embraced by national Democrats, despite the fact that Northam had support not only from most of the state’s Democratic elected officials from the governor on down, but also from pro-choice, gun control and LGBT advocacy groups.

Sanders, the Independent senator from Vermont who ran for president last year as a Democrat, Sen. Elizabeth Warren and others saw Perriello as a more energetic option whose anti-corporate economic message and passion for racial justice could win both working-class whites and African-American voters.

But in the end, Northam was able to rely on his network of elected officials to turn out traditional Democratic primary voters in an election where awareness of the contest was not high, and Perriello was not able to convert enough of the new voters who caused the party’s turnout to swell.

Perriello said in a concession speech that his campaign had “changed the conversation here in Virginia and elevated those who have been left out of the conversation for too long.” He pledged to fully support Northam and work for his election. Democrats have planned a unity rally for Wednesday morning in northern Virginia, and fully expect to see Democrats in Virginia and across the country rally to Northam’s side.

Stylistically, Gillespie and Northam are similar. Both are staid speech makers who have put in years of work in politics and have gotten results.

Northam is a well-liked and respected figure in Richmond among state legislators, and has been an effective partner for Gov. Terry McAuliffe over the past four years. He touted himself in the primary as able to work with the Republican legislature when the state needed to get things done, even if there are some things he said he wouldn’t compromise on.

Gillespie has long been a respected Republican operative, known for his smarts and decency, and he has tried to push the Republican Party in a more compassionate direction, most notably on immigration. For years he has worked to recruit more Latino candidates to run for elected office as Republicans. But in the closing days of the primary, he felt compelled to run digital ads saying that he opposed the removal of Confederate statues from public places.