Police shared that the incident happened when the Jewish student was about to enter the place of worship in the northern German city. He is said to be badly injured.
Germany’s Foreign Minister Heiko Maas called the attack as “repugnant anti-Semitism”.
Police assigned to protect the Hamburg synagogue have arrested a 29-year-old man who was wearing a uniform resembling that of the Germany army.
According to Germany's DPA news agency, they found a piece of paper bearing a swastika in one of his trouser pockets.
The victim managed to get away from the attacker and passers-by gave him first aid before he was taken to hospital, the agency added.
A police spokesman cited by DPA said the suspect, a German of Kazakh origin, seemed to be in a confused state, which made questioning him difficult.
Ronald Lauder, leader of the World Jewish Congress, denounced the attack, pointing out it came a year after two people were shot dead after an extremist tried to storm a synagogue in the eastern city of Halle.