title: Postgresql function to create table with dynamic table name?
body: <p>Let's say I have several users, each with their own set of contacts. Users get to select which 3rd parties have access to their contacts.</p>
  <p>I could maybe create 1 large contact table and have an 'owner' column so that I can identify which contacts belong to which users. However, I'd then need to maintain row-level permissions so that I can restrict which 3rd parties do/don't have access to specific user contacts.</p>
  <p>Instead, I think (and I could be mistaken here) it makes more sense to have a contact table for each user, e.g.: <code>contact_e878df81_eba1_4a61_b592_30ac7100362a</code>. I could then manage permissions in a separate table.</p>
  <p>To create these 'dynamic contact tables', I have the following function:</p>
  <pre><code>CREATE OR REPLACE FUNCTION create_contact_table(IN tbl_name text) RETURNS INT AS $$
  DECLARE
      table_name text;
  BEGIN
      table_name = tbl_name;
      create table IF NOT EXISTS table_name
      (
          id serial  not null
              constraint test_pkey
                  primary key,
          firstname varchar not null,
          lastname varchar not null,
          age int not null,
          address varchar not null,
          email varchar not null,
          created timestamp default CURRENT_TIMESTAMP
      );
      alter table table_name
          owner to postgres;
      RETURN 1;
  END ;
  
  $$ LANGUAGE plpgsql;
  </code></pre>
  <p>When I execute the function (<code>create_contact_table(contact_e878df81_eba1_4a61_b592_30ac7100362a)</code>, the table is created, but the name of the table is <code>table_name</code>...</p>
  <p>Why is that? How can I fix it so that the table name is correct?</p>