The defence against this is probably to put up some random junk between the planet and the ship. In all probability a grill of tungsten bars (or anything harder than the iron projectile) will fragment the projectile in the same way that some tanks have holes "drilled" into their armor to cause penetrating projectiles to be subjected to offcentre forces and therefore shear or shatter (it's called [STRIKE]spaced armor[/STRIKE]). [edit: I forget what it's called. But it exists]. [edit: I checked. It's "perforated armor". These are holes about 1/2 the expected diameter of the projectile, and causes the projectile to break up when it strikes]
You might or might not still have heat transfer from the fragments burning up in re-entry. I think that's a different problem.
I am fairly sure even a second hand "space motorbike" can tow such a device and can probably maneuver it more easily than an 8 million ton battlewagon. Likely you're going to need your own X-wing fighters after all. :-)
I find it hard to envisage a situation where such railguns are used but I have no first hand knowledge of interplanetary warfare(!) ... Or if I do I am keeping it secret until after we have captured all the Earthlings.
However, it seems only useful against planets with atmospheres and probably isn't the most effective way to force a surrender or kill all the life forms.
If they live underground, will it work?
If the planet is airless, will it work better than a much simpler and less complex bomb?
If the target is a ship, how do you detect, identify, target and aim? Most likely you are outside visual range. Can your radar distinguish a decoy e.g. an inflatable balloon with metallic coating, from a ship? A ship is pretty small in the vastness of space. How do you aim?
If the target is a ship, what happens if it's not solidly built, and the projectile enters one window, passes through the (empty) cargo space and then out the other window, doing nothing much? Happens plenty of times when you shoot an armor piercing anti-tank round at a jeep. The target does not have enough mass to convert the energy of the projectile.
Perhaps the option for an explosive projectile with a hardened nose cap, or even one that can shotgun for use against biodomes on an airless world?
BTW I see a lot of sci-fi that falls behind, or is not aware of, current military technology. For the benefit of readers with some knowledge of military systems it would be nice to cover why simple obvious defences like the aforementioned tungsten grids cannot be used.