Democrat Ralph Northam won the closely watched governor’s race in Virginia on Tuesday in a contest seen as a bellwether of the 2018 midterm elections and of President’s Trump’s political clout.

In what had been expected to be a photo finish, Northam won easily — 54 percent to 45 percent — over Republican Ed Gillespie, with 99 percent of precincts reporting.

“Today Virginians have answered and have spoken,” Northam told cheering supporters. “Virginia has told us to end the divisiveness — that we will not condone hatred and bigotry, and to end the politics that have torn this country apart.”

Northam, 58, is a pediatric neurologist and military veteran who treated wounded soldiers during Operation Desert Storm.

“I want to let you know that in Virginia, it’s going to take a doctor to heal our differences, to bring unity to our people,” he said. “And I’m here to let you know that the doctor is in!”

Gillespie wiped away tears and struck a conciliatory tone in his concession speech, belying the bitter, racially charged campaign that had just ended.

“As I said throughout the campaign, Governor-elect Northam is a good man and I appreciate his service to our country and commonwealth,” said Gillespie.

Democrats had sent in their biggest guns — former President Barack Obama and former Vice President Joe Biden — to stump with Northam, the lieutenant governor, while Trump issued a last-minute appeal from Asia to the GOP faithful to back Gillespie, a former Republican National Committee chairman.

After Gillespie lost, Trump said he ran a poor campaign, tweeting: “Ed Gillespie worked hard but did not embrace me or what I stand for. Don’t forget, Republicans won 4 out of 4 House seats, and with the economy doing record numbers, we will continue to win even bigger than before!”

Trump was a factor in the campaign, according to exit polls.

Among early voters, the president had a 55 percent job disapproval rating. And about one-third of voters expressed opposition to Trump, double the 16 percent who supported him.

Backed by the teachers union, Northam ran ads linking Gillespie to Trump’s education policy favoring charter schools.

Conversely, Gillespie, 56, who once served as counselor to former President George W. Bush, kept his distance from Trump.