Is it a good idea to leave a PhD program for another one in a better school after two years? I am finishing my second year as a PhD student in computer science and I have an opportunity to go to a much higher ranked school. Is it a good idea to just leave my current program to the future school? Or is it a better to wait and hope to get a good admission for post doc? How much the school prestige for PhD affects the future academic career?

My research is moving okay and I am done with all my coursework in the first year. It is not convenient to abandon all my work and just start over. But I want to know if it is worth moving to a better program for potential greater good.
I would argue it heavily depends on advisor. Are you not just moving to a "better" program but to an advisor who is well-positioned to help you get you the kind of job you want after you finish? If you move to a "better" program but one that does not match you to someone whose interests you share and who has a good track record of placements, it's hard to say whether it's a good move. Don't move for prestige alone; move only if it is likely to have a tangible impact on your career success.