Democrat Ralph Northam defeated Republican Ed Gillespie in Virginia’s gubernatorial election Tuesday, granting Democrats their first major victory, and Republicans their first big loss, since the election of President Donald Trump.

Northam beat Gillespie by a comfortable margin of 54 to 45 percent.

The stakes of the closely fought race were especially high for Democrats, who were desperate to hold on to a governorship in a state with a popular Democratic governor and where Hillary Clinton defeated Trump by 5 percentage points in 2016.

The outcome provides rank-and-file Democrats with a much-needed injection of optimism, averting the catastrophic scenario of another crucial loss.

It also deals a blow to the Trump-like right-wing populist playbook that Gillespie employed in his bid to win.

“Democrats were anxious all along, at the very end, about whether this thing would go their way, whether their core vote would turn out for them. And it did,” said Quentin Kidd, a political science professor at Christopher Newport University in Newport News, Virginia. “They’re breathing a sigh of relief.”

What’s more, the significant size of Northam’s win bodes well for Democrats across the country in the 2018 midterm elections, according to Kidd.

“Everything we thought was happening was actually happening,” he said. “There was a lot of pent up anger and frustration in the Democratic electorate.”

Win McNamee/Getty Images Democrat Ralph Northam defeated Republican Ed Gillespie in the nation's closest watched election on Tuesday.

Northam had held a modest advantage in an average of major polls on Election Day. But the wide variation in individual polls toward the end of the contest spooked many Democrats who had anticipated a more certain victory.

Northam, 58, a pediatric neurologist and the current lieutenant governor, ran a mainstream Democratic campaign that emphasized his all-American biography. Northam is a graduate of the Virginia Military Institute who served as an Army doctor during Operation Desert Storm before establishing a practice in southeast Virginia’s Hampton Roads region.

Northam campaigned on continuing the work of Gov. Terry McAuliffe (D), who has presided over sustained economic growth and restored the voting rights of 168,000 former felons.

Northam also proposed increasing public school funding, providing tuition-free community college education in key areas of study for students who pledge a year of service, and using Affordable Care Act funds to expand Medicaid.

Northam will have the important opportunity to play a role the next time the state legislature redraws congressional and state districts in 2021. As governor, he will have the power to veto a plan submitted by the Republican legislature if he believes the makeup of the new districts is unduly partisan.

More than any one policy outcome, however, Northam’s win delights progressives for what it denies the GOP: another vindication of Trump-like race baiting.

Gillespie, 56, a former chairman of the Republican National Committee and a Beltway lobbyist, once embodied the socially moderate, fiscally conservative establishment wing of the Republican Party. And to some extent, Gillespie maintained this profile, emphasizing economic policy on the stump, including a proposal to cut Virginia’s income tax rates by 10 percent across the board.

But in a bid to shore up a wayward right-wing base that nearly handed populist Corey Stewart the nomination in a tough GOP primary, Gillespie embraced the Trump playbook.

In a barrage of negative advertisements, Gillespie sought to paint Northam as a frightening radical who would enable convicted sex offenders, undocumented immigrants and members of the El Salvadoran gang MS-13 to run amok in the Old Dominion State. He also claimed that Northam wanted to take down the state’s Confederate monuments, despite Northam’s assurances that he would allow localities to decide.

Ultimately, Gillespie’s approach scared off too many of the voters he needed to win in the increasingly Democratic-leaning state.

“Gillespie tried to do two things: He tried to run on white identity politics and Republican economics,” Kidd said. “Republicans have to decide which of those two they’re going to run on because they probably can’t do both at same time.”