ATLANTA (Reuters) - U.S. Democrats elected former Labor Secretary Tom Perez as chairman on Saturday, choosing a veteran of the Obama administration to lead the daunting task of rebuilding the party and heading the opposition to Republican President Donald Trump.

Members of the Democratic National Committee, the administrative and fundraising arm of the party, picked Perez on the second round of voting over U.S. Representative Keith Ellison, a liberal from Minnesota.

Following one of the most crowded and competitive party leadership elections in decades, Perez faces a challenge in unifying and rejuvenating a party still reeling from the Nov. 8 loss of Democratic presidential candidate Hillary Clinton. He immediately made Ellison his deputy.

After losing the presidency and failing to recapture majorities in Congress, party leaders are anxious to channel the growing grassroots resistance to Trump into political support for Democrats at all levels of government across the country.

“We are suffering from a crisis of confidence, a crisis of relevance,” Perez, a favorite of former Obama administration officials, told DNC members. He promised to lead the fight against Trump and change the DNC’s culture to make it a more grassroots operation.

Perez, the son of Dominican immigrants who was considered a potential running mate for Clinton, overcame a strong challenge from Ellison and prevailed on a 235-200 second-round vote. Ellison, who is the first Muslim elected to the U.S. Congress, was backed by liberal leader U.S. Senator Bernie Sanders of Vermont.

The showdown between candidates backed by the establishment and progressive wings of the party echoed the bitter 2016 primary between Clinton and Sanders, a rift Democrats will try to put behind them as they turn their focus to fighting Trump.

Those divisions persisted through the months-long race for chair, as many in the party’s liberal wing were suspicious of Perez’s ties to the establishment and some Democrats raised questions about possible anti-Semitism in Ellison’s past.

Some Ellison supporters chanted “Not big money, party for the people” after the result was announced.

Slideshow ( 13 images )

But both Perez and Ellison moved quickly to bring the rival factions together. At Perez’s urging, the DNC suspended the rules after the vote and appointed Ellison the deputy chairman of the party.

“I am asking you to give everything you’ve got to support Chairman Perez,” Ellison told DNC members after the vote. “We don’t have the luxury, folks, to walk out of this room divided.”

‘TRUMP’S NIGHTMARE’

Perez said the party would come together.

“We are one family, and I know we will leave here united today,” Perez said. “A united Democratic Party is not only our best hope, it is Donald Trump’s nightmare.”

Trump took a dig at Perez and Democrats in a tweet offering his congratulations on the election.

“I could not be happier for him, or for the Republican Party!” Trump said.

Perez and Ellison wore each other’s campaign buttons and stood shoulder-to-shoulder at a news conference after the vote. Perez said the two had talked “for some time” about teaming up, and Ellison said they had “good synergy.”

Slideshow ( 13 images )

“We need to do more to collaborate with our partners in the progressive movement,” Perez said, adding he and Ellison would look for ways to “channel this incredible momentum” in the protests against Trump and against Republican efforts to repeal President Barack Obama’s healthcare plan.

Sanders issued a statement congratulating Perez and urging changes at the DNC.

“It is imperative that Tom understands that the same-old, same-old is not working,” Sanders said. “We must open the doors of the party to working people and young people in a way that has never been done before.”

The election offered the DNC a fresh start after last year’s forced resignation of chairwoman Debbie Wasserman Schultz, who stepped aside when the release of hacked emails appeared to show DNC officials trying to help Clinton defeat Sanders in the primaries.

Both Perez and Ellison have pledged to focus on a bottom-up reconstruction of the party, which has lost hundreds of statehouse seats under Obama and faces an uphill task in trying to reclaim majorities in Congress in next year’s midterm elections.

Perez said he would redefine the role of the DNC to make it work not just to elect Democrats to the White House but in races ranging from local school boards to the U.S. Senate, pledging to “organize, organize, organize.”

“I recognize I have a lot of work to do,” he said. “I will be out there listening and learning in the weeks ahead.”

Perez fell one vote short of the simple majority of 214.5 votes needed for election in the first round of voting, getting 213.5 votes to Ellison’s 200. Also on the first ballot were four other candidates -- Idaho Democratic Party Executive Director Sally Boynton Brown, election lawyer Peter Peckarsky, and activists Jehmu Greene and Sam Ronan.

Pete Buttigieg, the mayor of South Bend, Indiana, withdrew just before the voting, while Brown, Greene and Ronan dropped out after the first round.