AN off-duty policeman has prevented a possible terrorist bloodbath, arresting the alleged gunman after he stormed into a Spanish supermarket shouting “Allahu Akhbar” and opening fire.

The attacker was allegedly carrying gunpowder and petrol in a suicide vest.

The Sun reports the attacker walked into the Mercadona shop in the As Lagoas district of Ourense before firing several shots into the air.

Several shoppers were inside the supermarket when the man entered and fired six shots into the air — alerting emergency services to the attack.

But the gunman was stopped by off-duty policeman Carlos Perez, 38, who luckily was passing by as the attack unfolded.

“I saw a person with an open backpack and a shotgun in his hand,” Mr Perez told lavozdegalicia.es.

According to local media, the gunman shot at Perez but stopped when he saw the off-duty cop also had a gun.

One employee confirmed to the newspaper the gunman shouted the Arabic phrase for “God is great” before he opened fire.

Immediate reports indicate no one has been hurt and the gunman has been arrested.

Armed police arrived and reportedly led the attacker out of the shop, telling pedestrians to stand back.

Terrified customers hid for half an hour, fearing others were dead after hearing shots.

One witness told El Correo Gallego she thought it was a “joke” at first before realising what had happened, hiding with some others next to the fishmonger.

She said people were “very afraid” and were crying, not knowing if “there were people alive or dead”.

The woman said she heard the man make some proclamation when he entered - but witnesses said they were not sure exactly what he said.

The incident, which happened at around 2pm, did not result in any injuries but left shoppers extremely shaken.

After firing at Perez, the gunman - reported to be a 35-year-old man from Bilbao - started eating a banana before dropping to the ground, according to newspaper Faro de Vigo.

An employee said the man shouted “Allahu Akbar” - which means “Allah is great” - as he opened fire, reports La Region.

Police sources also told local media the man had gunpowder and petrol in his pockets, and La Region says officers emerged from the shop carrying a vest - warning onlookers it was armed.

Pictures from inside the supermarket show a double-barrelled shotgun, believed to be the weapon used in the attack.

Other photos show the aftermath, with broken glass and debris covering the shop floor.

According to Spanish media outlet La Region, police officer Perez said he and his colleagues were able to detain him after he ran out of ammunition.

Asked about the man screaming ‘Allahu Akbar’, the officer told La Region he had heard him say something in Euskara - the language of the Basque region - but didn’t understand it.

This article first appeared in The Sun.