Greek Prime Minister Andreas Papandreou today threatened to veto the entry of Spain and Portugal into the European Community unless his demands are met for a $5 billion, five-year aid program for the poorer countries of the Mediterranean region, of which Greece would be a major beneficiary.

While saying "we are very far apart" on the Greek demands, Irish Prime Minister Garret FitzGerald, the current EC president, said at a press conference that the two-day summit had "in many ways been a success."

That view was also expressed by British Prime Minister Margaret Thatcher, West German Chancellor Helmut Kohl, and Spanish Foreign Minister Fernando Moran. Moran issued a statement in Madrid.

The leaders, including Papandreou, finally reached agreement after months of negotiations at lower levels on such vital issues as fishing rights and how to limit wine production among EC member countries that already produce excessive quantities of wine.

FitzGerald acknowledged that Papandreou's demand today created "a real problem . . . it leaves a measure of doubt whether, when the negotiations are concluded [with Spain and Portugal] the agreement will be put into effect."

Several of the Western European leaders meeting here made it clear at the end of the summit that Papandreou's demands for the Mediterranean program were greatly in excess of anything the current 10-nation community had in mind or could afford.

Thatcher said at a press conference that the figures were "so far out they should never have even been mentioned," and French President Francois Mitterrand reportedly decribed them as "absurd" and a "fantasy" in the closed-door sessions.

The row with Papandreou, who at one point walked out of the meeting, caused today's session to run five hours longer than planned.

Although diplomats said they expected the differences with Greece to be worked out, Papandreou's action put a damper on a summit meeting that the other nine members, plus Spain and Portugal, were about to hail as an unqualified success.The EC also agreed:

* to pledge $330 million in food aid next year to famine victims in Africa.

* to urge a new peace initiative in the Middle East, recognizing the Palestine Liberation Organization as a negotiator.

Because Spain and, to a lesser extent, Portugal are also big wine producers, working out an acceptable formula for the wine cutbacks had become the chief stumbling block to final negotiations that would allow those two countries into the community by the Jan. 1, 1986, deadline.

Moran hailed the agreement reached here as a breakthrough that "means the negotiations on Spanish entry are unblocked."

Much more than wine subsidies is at stake. A rebuff to Spain by the EC, often referred to as the Common Market, could also cause Spanish voters, in a referendum next year, to reject continued membership in NATO.

FitzGerald, Thatcher and Mitterrand said it also means that it will now take until March, when the next EC summit is scheduled, to clear away all obstacles to Spanish and Portuguese entry.

As FitzGerald explained it, the main issues have been agreed upon in principle, and negotiations on the details can now go forward with Spain and Portugal.

But Greece has placed a "reservation" on the right to veto enlargement of the Common Market until it is satisfied with what it termed an "Integrated Mediterranean Program."

Between now and March, the negotiations with Spain and Portugal are to go forward. That will still "leave ample time," FitzGerald said, to meet the January 1986 deadline. During that period, the other nine countries would try to reach a compromise with Greece to prevent a veto.

Thatcher said today that at the moment, the EC has only talked about the first year of a five-year Mediterranean program. While she would give no details, diplomats said the community was only willing to come up with about $65 million.

Another potential difficulty is that West Germany, which has pressed hard for the admission of Spain and Portugal, has threatened to refuse payment of its share of increased budget costs next year if their membership is blocked.

While the agreement on curbing Europe's heavily subsidized wine glut to pave the way for Spanish and Portuguese entry was viewed as an important achievement, the events of the past two days also illustrated the problems that continue to trouble the Common Market and cloud the question of European unity, in the view of some observers here.