Greek Prime Minister Alexis Tsipras survived a no-confidence motion in parliament on Saturday, setting the stage for the signing of a historic accord with neighboring Macedonia to settle a long dispute over the latter's name.

The motion brought by the opposition New Democracy party was rejected by 153 MPs, with 127 in favor. Political opponents had accused Tsipras of making too many concessions over the deal, due to be signed on Sunday.

Thousands of Greeks protested outside parliament against the accord with Macedonia, calling for Tsipras to resign. Police used stun grenades and tear gas to prevent them from entering the building.

"This is a deal I believe that every Greek prime minister would want," Tsipras told the chamber earlier.

Had he lost, the leftist elected in 2015 would have had to relinquish his mandate to the country's president, signalling early elections. He is already trailing centre-right New Democracy in opinion polls.

Greece had been in dispute with Macedonia since 1991 over the former Yugoslav republic's name, arguing it could imply territorial claims over the Greek province of Macedonia and an appropriation of ancient Greek culture and civilization.

The subject is a deeply emotional one for many Greeks. On Saturday, protesters outside the parliament building shouted "traitor, traitor!" as lawmakers debated inside.

"I'm just furious," said Theologos Ambotis, 69, a monk. "Conceding on the name is conceding on territory. Macedonia and Alexander the Great is Greek history and culture, and they are just giving it away to Skopje."

Under the terms of the accord, the country will be known as "Republic of North Macedonia" and Greece will lift its objections to the renamed nation joining the European Union and the NATO military alliance.