Spain's caretaker Prime Minister Pedro Sánchez won MPs' backing to form a left-wing coalition government.

The vote ends almost a year of political gridlock but the vote was, as expected, extremely close with 167 votes in favour of the coalition and 165 against.

The acting prime minister won largely thanks to 18 abstentions.

The ERC — Catalonia’s largest separatist party — promised to abstain in exchange for talks over the future of the region.

Sánchez and Pablo Iglesias, who leads the far-left Unidas Podemos, or United We Can Party, recently restated their intention to form the first coalition government in Spain’s recent history.

This is the first coalition government since Spain's transition to democracy after the regime of Francisco Franco.

Sánchez, leader of the Socialist party, lost an attempt on Sunday to get the backing of his coalition government.

He had needed 176 votes for an absolute majority but instead received 166 in favour and 165 against.

One Podemos MP was absent on Sunday — Aina Vidal — who is battling cancer. She received a standing ovation on Tuesday.

Sanchez, 47, has been caretaker prime minister since early last year.

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

Sanchez's Socialists won two consecutive general elections in 2019, but both times they failed to capture a parliamentary majority. That meant they couldn't win the parliamentary confidence vote that is required before taking office.

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

Andrew Dowling, an expert on Spanish contemporary politics at Cardiff University in Wales, said Sanchez attained power through ``a very fragile agreement'' with other parties and will now be ``subject to intense political pressure'' by his right-of-centre political opponents.

`"They're not going to give any quarter to this new government (which has) a very, very fragile majority. They are going to use all means necessary to prevent this government passing legislation,'' Dowling said in a telephone interview.

Dowling said he didn't expect any radical policies from the new administration, predicting it will present ``a moderate social-democratic programme focusing on workers' and women's' rights.

Sanchez 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 Sanchez for striking deals with parties intent on breaking up Spain, though Sanchez has insisted he won't allow the wealthy region's secession.

Pablo Casado, leader of the main opposition centre-right Popular Party, described 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.

Dowling, the analyst, said Sanchez's promises to the ERC will expose him to attacks from the right. ``Any concessions to the Catalan independence forces will be like a red rag to a bull'' to his opponents, Dowling said.

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 Tuesday he needed only a simple majority - more votes for him than against him.

King Felipe VI asked Sanchez to try and form a government following the Nov. 10 ballot, when the Socialists got the most votes but only 120 seats in the 350-seat Chamber of Deputies.

Sanchez and pony-tailed 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.

Iglesias, whose party was created just three years ago, wrote on his Twitter account, ``Yes, we can.''

Until recent years, Spanish politics had been dominated by the Socialists and the Popular Party. But that began to change when Europe's recent financial crisis gave rise to new parties, including United We Can and most recently the far-right and anti-immigrant Vox, now Spain's third-largest parliamentary group.

Besides opening talks with the Catalan separatists, Sanchez's first challenge will be to get a 2020 state budget through parliament.