The Faroe Island like Iceland was a part of the Danish kingdom so in principle they were under the protection of the Danish armed forces – and up here it would primarily be the Danish navy which actually was one of the biggest in Europe up until the 19th century. But the navy mainly stayed in Denmark – so up here they rarely saw any Danish naval vessels.
With no major naval present in the area pirates had a free reign of the north Atlantic. And pirate did attack the secluded beaches of Iceland and the Faroe Islands occasionally. Sometimes it was Algerian pirates taking dozens of villages prisoners to sell them on the slave markets in North Africa. This risk must have been terrifying for the people of Thorshavn so they were probably happy to see a fort being built to deter piracy.
The old fort of Thorshavn is called Skansin and is located next to the harbor with a great view of the ocean. The fort is small and it is free to go and visit the ground. I guess even a small fort was enough to deter pirates from attacking the town – they would probably just sail on and try to find an easier target other places on the Faroe Islands or Iceland.
The original fort was built in 1580 but has later been expanded considerably in 1780. During the Second World War the fort was used as a British base in Thorshavn so it remained in service for almost 400 years protecting the harbor of the city.
It is nice to wonder a bit around the stone buildings with grassy roof and see the stone bastions of the fort. From the fort you also have a decent view of parts of Thorshavn.