Isn't it a greater risk to buy bonds when the government is in debt?
That depends on the nation, which is issuing the bonds. Some of the countries issue bonds which are having a very low risk of default, such as the United States. But these bonds usually have very low interest payments, like 3% per year. On the other hand, there are bonds issued by "risky" nations, which are having higher interest rates.
I think the government bonds exists since a long time and I believe that people have more faith in Government bonds as in the past there were no more options available for the investments and people were sure that their money is safe by investing in the government bonds.