I want to create insert script for table Postgres database. The table should look like this.
  id    |    refid                  name
------------------------------------------------------------
autoinc |    null                |      admin
autoinc |    null                |      moderator
autoinc |    id of moderator     |      readonly
autoinc |    id of moderator     |      groupadmin
autoinc |    id of groupadmin    |      rolesadmin
autoinc |    id of rolesadmin    |      users
autoinc |    id of users         |      null
My problem is that I do not know how use just inserted id and re use it in the rest of the script. For example I want to insert row with moderator name and use moderator's id in 2 inserts below.
How can I achieve that?
I have tried with
WITH moderator AS (
    INSERT INTO table_name
        (refid, name)
    VALUES
        (null, 'moderator')
    RETURNING id
) 
INSERT INTO sd_roles (refid,rname)
VALUES   ((SELECT id FROM moderator), 'groupadmin')
But it is getting nested more and I would need to write with in with.
 
     
     
    