Spain’s parliament has chosen Socialist leader Pedro Sanchez to form a new government, ending almost a year of political limbo for the eurozone’s fourth-largest economy.

He won a cliff-hanger confidence vote 167-165, with 18 abstentions – the slimmest victory for a prime minister candidate in decades.

His party will lead a leftist coalition with the United We Can party as junior partner.

Mr Sanchez needed the votes or abstention promise of an array of smaller parties to win. His supporters in parliament erupted into a standing ovation when the result of the vote was announced in the Chamber of Deputies.

The hairline margin of victory has raised doubts about how long the coalition government will last, as its policies require regular parliamentary approval. A government term is normally four years.

Spain Politics
Pedro Sanchez applauds with members of the Socialist party (Manu Fernandez/AP)

Coalition governments are common in Europe, but it is Spain’s first since the country returned to democracy in 1978, three years after the death of long-time dictator General Francisco Franco.

“A progressive coalition is going to govern because that is what Spaniards decided,” Mr Sanchez told the house before the vote.

The 47-year-old has been caretaker prime minister since early last year.

He is expected to be sworn in Wednesday and to hold his first cabinet meeting of ministers on Friday.

His Socialists won two consecutive general elections in 2019, but both times they failed to capture a parliamentary majority. That meant they could not win the parliamentary confidence vote required before taking office.

In weeks of negotiations since the last election in November, Mr Sanchez mustered enough support — or promises to abstain — from a handful of small regional parties to take power.

But he has been widely criticised for the deal he clinched with the regional Catalan ERC party for it to abstain in Tuesday’s vote. ERC, which holds 13 seats, is one of several groups that want Catalonia’s independence from Spain.

Opposition parties, most of them right-wing, have lambasted Mr Sanchez for striking deals with parties intent on breaking up Spain, though he has insisted he will not allow the wealthy region’s secession.

Pablo Casado, leader of the main opposition centre-right Popular Party, described Mr Sanchez as an “extremist” for putting the government in the hands of “terrorists and coup supporters” in references to the deals made with Basque and Catalan separatist parties to abstain in Tuesday’s vote.

The Catalan independence push has brought Spain’s most serious political crisis in decades.

The Socialists defend the deal with ERC, saying the Catalan crisis must be resolved through talks, something they have agreed to do with the ERC.

Mr Sanchez tried to get elected in a first parliamentary confidence vote last Sunday, but he fell far short of the target of 176 votes. Under Spanish law, in the second round of voting on Tuesday he needed only a simple majority — more votes for him than against him.

King Felipe VI asked him to try to form a government following the November 10 ballot, when the Socialists got the most votes but only 120 seats in the 350-seat Chamber of Deputies, Spain’s parliament.

Mr Sanchez and United We Can leader Pablo Iglesias say they want to raise minimum salaries, income tax for high earners and capital gains tax. They also vow to defend women’s and immigrants’ rights.