Police said the man invoked the name of Allah during the assault, which came just days after the first anniversary of a twin attack in Catalonia that killed 16 people.
The man arrived at the closed police station in the town of Cornella de Llobregat near Barcelona at 5:45 am (0345 GMT) and repeatedly pressed the buzzer to be let in, Rafel Comes, a commissioner with the Catalan regional police, told a news conference.
After police allowed him in, the man pulled out a “large knife” and lunged at officers in “a clearly premeditated desire to kill an agent of our force,” Comes said.
“The officer used her gun to save her own life,” Comes said, adding that “we are treating it as a terrorist attack”.
The man shouted “Allah” as well as words the police officers did not understand, Comes said.
A man armed with knife has acceded this morning to Cornellà's police station with the aim to attack the Police officers. The aggressor has been shot down. The facts happened a few minuts before 6 a.m.
— Mossos (@mossos) August 20, 2018
Anti-terrorism police sources had earlier told AFP the man was a 29-year-old Algerian, but Comes said police still needed to confirm that the Algerian identity papers he carried with him were in fact act his.
Police 'monitored couple'
Officers searched the man's home, which was located just a few hundred metres (yards) from the site of the attack.
Resident Conchi Garcia, a 50-year-old office worker, said the man suspected in the attack came to the neighbourhood two years ago, moving in with a single mother who had always lived in the area.
“She started wearing the veil overnight. It was strange,” Garcia told AFPTV.
Another neighbour, who would only give her first name, Mary, said police “monitored the couple's movements closely”.
Security has been reinforced at police stations across Catalonia.
The incident occurred just days after the one-year anniversary of a deadly jihadist rampage in Catalonia.
Sixteen people were killed on August 17th and 18th, 2017 when a van drove into crowds on Barcelona's popular Las Ramblas boulevard and in a knife attack in the nearby resort of Cambrils.
The Islamic State group claimed responsibility for the attacks, Spain's worst since the Madrid train bombings in 2004 when 191 people died and more than 1,800 were injured.
Spain has kept its terrorist alert at the second-highest level since 2015.
'Centre of jihadist activity'
Catalonia, which is home to a significant number of second-generation North African immigrants, has had a long history of Islamic militant activity.
Spain's first Muslim extremist — a member of the Algerian Armed Islamic Group (GIA) — was uncovered in Catalonia in 1995.
Mohammed Atta, the pilot who slammed a passenger plane into one of New York's World Trade Center towers on September 11, 2001, spent time in Catalonia shortly before the attacks.
And in 2008, police foiled a plot targeting Barcelona's underground trains.
One in four people detained in Spain in relation to extremist Muslim-linked terrorism come from the Catalan province of Barcelona, according to a study published last year by the Real Instituto Elcano, a Spanish think-tank, which called the province the country's “main centre of jihadist activity”.
There have been a string of similar incidents in neighbouring France targeting police and soldiers, including one in January 2016 in which police shot dead a man wielding a cleaver and yelling “Allahu akbar” as he tried to attack a police station in northern Paris.
The Islamic State group has frequently called on their followers to attack soldiers and police in France, who they see as a legitimate target because they represent the French state.
Separately on Monday, three people were seriously injured when a car mounted a pavement in the northern Spanish city of Zaragoza and sped off, sparking fears of a vehicle attack.
However a local police spokesman said it was a road accident, and the two occupants of the car were later detained.