While he is favored to win, Lt. Gov. Ralph S. Northam, the Democratic candidate for Virginia governor, is under duress from both his Republican opponent and his own left flank in ways that could foreshadow his party’s challenges in next year’s midterm races.

ANNANDALE, Va. — Propelled by demographic changes that are turning Virginia into an increasingly blue state and a liberal base energized by the convulsive Trump presidency, Democrats have long been favored to retain the governorship here when the state goes to the polls on Tuesday.

But a racially tinged, divisive campaign by the Republican nominee, Ed Gillespie, who has transformed himself from establishment fixture to culture warrior, has tightened the race and, perhaps, presented a template for how to run a state campaign in the Trump era.

And while he is still favored to win, Lt. Gov. Ralph S. Northam, the Democratic candidate, is under duress from both his Republican opponent and his own left flank in ways that could foreshadow his party’s challenges in next year’s midterm races.

For all the talk of Republican divisions, the party of President Trump has largely rallied around Mr. Gillespie’s confrontational candidacy in what is probably the most consequential election since Mr. Trump took office. While Mr. Gillespie kept the president out of Virginia, the only Southern state Mr. Trump lost, he has run a campaign that gave Mr. Trump much to admire.

“He’s closed an enthusiasm gap by rallying around the Trump agenda,” said Stephen K. Bannon, the president’s former strategist. “And I think the big lesson for Tuesday is that, in Gillespie’s case, Trumpism without Trump can show the way forward. If that’s the case, Democrats better be very, very worried.”

Mr. Gillespie, who just a decade ago warned his party against the “siren song” of weaponizing immigration, has poured millions into racially tinged television ads and mailings that excoriate Mr. Northam on that issue, as well as Confederate monuments, the restoration of felons’ rights and even football players who kneel during the national anthem.

This onslaught over issues of culture and identity, a mix of the Trumpian tactics of today with the unvarnished appeals from the past in a state defined by race since Jamestown, has appalled Democrats in Virginia and beyond.

The gut-punch approach has left even some Republicans wincing over the spectacle of a former Republican National Committee chairman and New Jersey native trying to win with earnest vows to guard emblems of the Lost Cause and with warnings about menacing Hispanic gangs.

Yet Mr. Gillespie’s strategy has brought him within a few points of Mr. Northam in both public and private polling. A New York Times Upshot/Siena College poll, released on Sunday, showed Mr. Northam with just a three-point lead over Mr. Gillespie, 43 percent to 40 percent.

That has forced nervous Democrats to confront the reality that race, immigration and crime can be a potent mix even in a rapidly suburbanizing state where Hillary Clinton won by more than five points last year and where, overall, the president remains deeply unpopular.

“I don’t think it will work in Virginia, but I just don’t know,” said Representative Robert C. Scott, Virginia’s first black congressman since Reconstruction.

Should Mr. Gillespie win or narrowly fall short, he will have handed 2018 candidates in competitive races a playbook for Trump-era campaigns: deploy the president’s politics but avoid Mr. Trump himself.

Democratic officials have been especially troubled as their ostensible allies have responded with ham-handed scare tactics of their own and demanded that their candidates reject any policies that carry even the whiff of Trumpism.

In an attempt to stir nonwhite…