Updated: Nov 29, 2019
It is a bit of a cultural joke, dogs do not seem to like the mailman. (NOTE: mailman is used because of familiarity. It is not meant to be gender specific, but gender neutral.)
Lets take a look at it from the dog's perspective. A human comes to the house in a uniform usually with a hat and sometimes with dark glasses. Most of the time they are carrying something. All of these things are a bit threatening to a dog. Many dogs will then bark as the spooky human makes their way into the dog's territory. They spend sometime at the door and then they leave.
If the dog barked throughout this progression, then in the dogs eyes the barking worked. The dog thinks, "if I bark at the mailman or whoever comes to the house, they will eventually leave. Job accomplished!" If the dog repeats this process enough times the dog is convinced they are solving this problem of the mailman entering their territory. And if they bark louder and more intensely perhaps they can prevent the mailman from coming at all.
The dog is not barking because they are excited to see the mailman. Oh to the contrary! If this is allowed to continue the dog can get itself into a tizzy that could cause it to bust out of the house to then attack the mailman. And yes, unfortunately that does happen. This can all be prevented but it requires some intervention.
If there is no way to control your dog when the mailman comes then the dog needs to be prevented from seeing the mailman approach the house. This will reduce their anxiety while home alone. They should be kept in a room as far from the front door as possible. A covered crate is another great place the dog can go for safety.
If there is someone home when the mailman approaches the house and the mailman is willing to help the situation. The mailman can come with treats you have provided in advance. If the dogs are behind a fence the mailman can toss the treats to the dogs upon approach. Repeat this enough times the dogs will soon look with anticipation for the mailman to come. It is important the mailman tosses the treats for two reasons. The first is to make sure the mailman is the source of the treat and the other is to prevent further fear of the mailman by keeping their distance. When someone approaches a dog with a treat and bends over reaching out with a treat to give to a dog is a VERY threatening action to a dog.
This can also be achieved without the mailman "paying" the dogs. The dogs can be taught to go to a particular room in the house when the mailman approaches. Every time the mailman approaches, call the dogs into the room with exciting treats and treat them until the mailman leaves. Repeat this enough times the dogs will run to the room as soon as the mailman arrives at the driveway.
So remember dogs don't really hate the mailman. They have just been conditioned to do the only thing they know how to do.