Military industries and structures cost much more than you lose to raids.
This is what trips people up.
New colonies can be profitable just by existing from population taxes, even more so if they have access to farming or mining exports.
But
all of that income can be wiped out several times over by just building random stuff all over the colony and racking up maintenance costs.
And the more hazardous the world the quicker this compounds, so the fewer things you can build and stay in the black.
Running colonies isn't just about knowing what to build, but also
when to build.
Your first colony wants to be as minimalist as possible starting out - a mine or farm, plus a level 1 station. That's it. Nothing more.
Avoid the temptation to build everything immediately.
Patrol HQ is the big maint. hog that usually causes problems early on. (Patrol has a maint value of 8 vs a station that has a value of 3.)
You definitely do need to build a HQ eventually, but you don't need it
immediately. Wait until you have a decent income to cover the cost.
It doesn't really matter if your colony gets raided though, it's only a temporary stability loss that should be easily absorbed.