When should you build defensive armies in Stellaris? And how should you work out how many to build? At the moment (2260) I have no defensive armies anywhere.
Old question with a new answer since 2.2 "LeGuin":
With the 2.2 rework of planetary population, defensive armies are no longer built directly, they are spawned automatically by certain jobs and buildings -- by default, that'd be the capital, police precincts and military forts/academies, the 'soldier' and 'enforcer' jobs.
Strictly speaking, I haven't found a lot of use for stacking massive amounts of defensive armies - if you're not at war they're not of much use and there are better jobs for your pops to be doing, and when you are at war it's a lot more cost effective to invest in space superiority so invaders won't get a chance to land in the first place.
Studoku correctly reminds me that the jobs that produce defensive armies also have side benefits - enforcers reduce crime, soldiers increase the naval cap, fortresses provide housing /and/ jobs, which may help overcrowding problems. Whether that's worth a building slot is up to you.