The man was subsequently detained, Spanish Interior Minister tweeted, without indicating whether the assault was a terror attack or not.
The man was subsequently detained, Spanish Interior Minister Juan Ignacio Zoido tweeted, without indicating whether the assault -- which took place early Tuesday -- was a terror attack or not.
"A man entered the border post and once inside, pulled out a large knife and confronted (police) shouting 'Allahu Akbar' (God is Greatest), slightly injuring a policeman," Irene Flores, spokeswoman for the central government's representative office in Melilla, told AFP.
A spokesman for Spanish police added he ran into the border post.
Flores said that an early investigation suggested the man was Moroccan, but that this had not yet been confirmed.
Melilla and its sister city Ceuta are two Spanish territories located on Morocco's northern coast, and as such represent the only two land borders between Africa and the European Union.
They have been hit by unrest before as migrants desperate to reach Europe regularly storm the border between Morocco and both territories or try and smuggle themselves in.
The Melilla border has been hit by several car-ramming incidents this year, in which people drive vehicles with migrants hidden inside into the border post at high speed.
But this is believed to be the first attack of this type.
Spain has so far been spared the kind of extremist violence that has occurred in nearby France, Belgium and Germany.
But it was hit by what is still Europe's deadliest jihadist attack in March 2004, when bombs exploded on commuter trains in Madrid, killing 191 people in an attack claimed by Al Qaeda-inspired extremists.
Since 2016, Spain has emerged as a potential target for jihadists, with extremist websites mentioning it for historical reasons, since much of its territory was once under Muslim rule.
Muslims settled there in the eighth century and ruled over part of the peninsula, particularly under the Caliphate of Cordoba in the 10th and 11th centuries and the Nasrid dynasty in southern Granada.
But they were forcibly converted to Christianity in the 16th century, and subsequently expelled from Spain.
Spain has nevertheless been less exposed to the risk that radicalised citizens who left to fight abroad would return with plans to commit attacks on home soil.
Only around 160 Spaniards are estimated to have joined the Islamic State group in Iraq and Syria, according to a study by the Real Instituto Elcano think tank, compared with over a thousand from nearby France since 2012.