Using Transactions with PostgresQL dependency issue
You don't need to query the table, to get the generated id, you can get that using the lastval() function:

insert into company_profile (....)
values (...);
insert into related_table(company_id, ...)
values (lastval(), ...);