I'm trying to create a new table in a schema that has multiple columns, named via an index.
CREATE TABLE rflux (pk SERIAL PRIMARY KEY NOT NULL);
Now I want to add new columns like col0 FLOAT, col1, col2, col3, .... up to colN.
I know I can do something like
ALTER TABLE rflux add column col0 FLOAT add column col1 FLOAT ... ;
but I don't want to type everything out, since I need to create ~4500 columns. I'm trying to implement this with loops but I can't quite get it working. Does anyone have any ideas? I've tried creating a function to do this ..
create function addColumns()
returns void
as $$
begin
for i in 1..10 loop
alter table rflux add column tmp float;
alter table rflux rename tmp to col||i;
end loop;
return;
end;
$$
language plpgsql;
then do select * from addColumns();
but I get errors when renaming the column to col||i , or even just when I try i. I'm not even sure if this is the best way to do this. How do I add multiple columns where I can increment the name of the column with a counter?
Edit..I know I can't do this with 4500 columns, but what's the solution to this problem if I wanted to do it for 10 columns, say?
Thanks.