Data migration: copy data with insert select and fill unique column
Why not use row_number()

insert into bar (txt, n)
select txt, row_number() over ()
from foo;


I left out an order by which would slow down the query and wouldn't really be an advantage in your case as you just want a unique number. 

An alternative would be to create a sequence for it, if the performance of the window function isn't sufficient.

create sequence seq_foo_nr;

insert into bar (txt, n)
select txt, nextval('seq_foo_nr')
from foo;

drop sequence seq_foo_nr;


If you need an auto-generated number anyway, then define nr as an identity column:

create table bar (
  txt text,
  n integer not null unique generated always as identity
);

insert into bar (txt)
select txt
from foo;