BBC: Ghana's new leader takes office
Election winner John Atta Mills gives thanks for his victory in church in Accra, 4 January John Atta Mills has pledged to be a "president for all"
John Atta Mills has been sworn in as Ghana's new president following a cliff-hanger election victory.
Mr Atta Mills took his oath of allegiance in front of thousands of people in Independence Square for the inauguration in the capital, Accra.
The National Democratic Congress (NDC) candidate beat the ruling party's Nana Akufo-Addo in a hotly contested poll by a margin of less than 0.5% of votes.
President John Kufuor has stood down after serving the maximum two terms.
He is the second elected head of state in Ghana's history to hand over to an opposition politician.
The BBC's Will Ross in the Accra says there is a carnival atmosphere in the capital.
Crowds began forming at the stadium before dawn - decked out in the national colours of green, yellow, red and black - amid an air of intense excitement after one of the closest election races in Africa's recent history.
Mr Atta Mills was dressed in a local kente cloth woven in the national colours, Reuters news agency reports.
After he was sworn in, he raised up the State Sword - representing government authority - to cheers from the crowd, the agency said.
A military unit then fired off a booming 21-gun salute.
Profile: John Atta Mills
Party: National Democratic Congress
Executive posts: Vice-president 1997-2000
Profession: University professor
Hobbies: Hockey, swimming
Family: Married with one child
On the eve of the inauguration, President Kufuor showed his successor around his new home, the presidential residence officially known as Jubilee House.
Ghana's new leader, who had lost two previous elections to Mr Kufuor, has pledged to be "a president for all".
Mr Akufo-Addo won the first round but not by enough to avoid a run-off in last month's knife-edge polls.
Mr Atta Mills was finally declared the winner on Saturday after a re-run of voting in the rural constituency of Tain, which was boycotted by the NPP.
Despite allegations of multiple voting and intimidation from both sides, electoral officials found no evidence of foul play and monitors praised Ghana's poll as a democratic example to others.
The stakes were raised further in the election by Ghana's recent discovery of crude oil, with production due to start in late 2010.
Turnout was high for Ghana's fifth set of polls since it embraced multi-party democracy in 1992.
The former British colony was the first African state to gain its independence in 1957.
A nation of 22 million people, it is the world's second biggest cocoa grower and Africa's number two gold miner.