US president Donald Trump and his Republican allies suffered stinging defeats on Tuesday as Democrats were projected to win closely watched elections in two states, results that signal troubling headwinds for his 2020 re-election campaign.

The Republican Governor Matt Bevin in deep-red Kentucky was ousted by his Democratic challenger Andy Beshear, who led by less than half a percentage point with 100 per cent of the votes counted, the official overseeing the election declared.

Doubling the hurt, Trump’s party also lost control of both chambers of the legislature in increasingly blue Virginia, US media including the New York Times projected.

“We have called it for Attorney General Beshear to be the Kentucky governor-elect,” Secretary of State Alison Lundergan Grimes said on CNN.

The US president tweeted that Bevin “picked up at least 15 points in last days, but perhaps not enough (Fake News will blame Trump!)”.

Beshear, whose father was the last Democratic governor in the state, claimed victory, but Bevin did not throw in the towel.

“This is a close, close race. We are not conceding this race by any stretch,” the governor said.

Should Bevin’s loss be certified, it would be a shock defeat for a conservative in a southern state that Trump won by 30 percentage points in 2016.

In Virginia, Democrats will now hold all major state-wide offices and rule the state assembly, a comprehensive consolidation of power not seen in the state since the 1990s.

Democratic leadership swiftly portrayed the night as a massive boost for the party heading into next year’s monumental battle against the president.

“This historic victory should send a chill down the spines of Donald Trump and every Republican,” Democratic National Committee chairman Tom Perez said in a statement.

“Democrats are competing in every election and every state, running on our values and channelling unprecedented energy into the voting booth – that’s how we won tonight, that’s how we’ll beat Trump” in 2020.

Tuesday’s elections – including a governor’s race in Mississippi that US media projected Republican candidate Tate Reeves would win – were tests of enthusiasm ahead of 2020 for Trump, who is deeply unpopular nationwide and is the subject of an impeachment investigation.

Trump hailed the Mississippi results, congratulating Reeves on Twitter and claiming that his support was key.

“Congratulations to @tatereeves on winning Governor of the Great State of Mississippi. Our big Rally on Friday night moved the numbers from a tie to a big WIN. Great reaction under pressure Tate!” Trump wrote.

With Washington swept up in the impeachment saga, results in Kentucky, Mississippi and Virginia were being closely watched for how the crisis is influencing their voters, how strong the support is for Trump in Republican bastions and whether Democrats are wielding increasing influence in the suburbs.

The Kentucky result – boosted by strong Democratic turnout in suburban districts outside Lexington and other major cities – is all the more humiliating for Trump because he flew there on Monday night to hold a large rally and implore his base to come out to the polls.

“If you lose, it sends a really bad message,” he said. “You can’t let that happen to me.”