Store select query’s output in one array in postgres

There are two ways. One is to aggregate:

SELECT array_agg(column_name::TEXT)
FROM information.schema.columns
WHERE table_name="aean"

The other is to use an array constructor:

SELECT ARRAY(
    SELECT column_name 
    FROM information_schema.columns 
    WHERE table_name="aean"
)

I’m presuming this is for plpgsql. In that case you can assign it like this:

colnames := ARRAY(
    SELECT column_name
    FROM information_schema.columns
    WHERE table_name="aean"
);

Leave a Comment