Having earthquake insurance is not mandatory even when you have a mortgage on the home.
Although when you have a mortgage you're required by the bank or mortgage lender to have standard homeowners insurance coverage on the home until the mortgage is paid off.
But the mortgage company typically doesn't require you to have the additional earthquake insurance coverage on the insurance policy.
However if you live in an area that is prone to earthquakes and the earthquakes have damaged homes severely in your location then it would be a good idea and wise to have earthquake insurance.
Just like if your area is prone to flooding you really should buy additional coverage and have flood insurance to cover damages due to floods and earthquakes.
If I lived in an area that got a lot of earthquakes I certainly would add additional earthquake insurance coverage to my homeowners insurance policy.
But if the location rarely got any earthquakes then I would not bother getting earthquake insurance.