Update as of PostgreSQL 9.0:
The new version of Postgres will have the string_agg()
function that will do exactly what the question asked for, even letting you speficy the delimiter string: PostgreSQL 9 General-Purpose Aggregate Functions
Original Answer (for pre-9.0 Postgres)
I've run into this before also. There is no built-in aggregate function to concatenate strings. It seems like this would be needed all the time, but it's just not part of the default set.
I Googled and found the same example:
CREATE AGGREGATE textcat_all(
basetype = text,
sfunc = textcat,
stype = text,
initcond = ''
);
Here is the CREATE AGGREGATE documentation.
In order to get the ", " inserted in between them without having it at the end, you might want to make your own concatenation function and substitute it for the "textcat" above. Here is one I put together but haven't tested:
CREATE FUNCTION commacat(acc text, instr text) RETURNS text AS $$
BEGIN
IF acc IS NULL OR acc = '' THEN
RETURN instr;
ELSE
RETURN acc || ', ' || instr;
END IF;
END;
$$ LANGUAGE plpgsql;
(edit - changed "varchar" in my function to "text" as pointed out by Kev)