Your Idea
The create_table() function will:
- open the connection
- set the role
- execute the SQL command
- close the connection
The parameter to set the role will be NULL as default which means the original user is used or the name of the role will be provided and the function will set it to that role.
The difference in this function is the role is set before the SQL command is executed, this will allow new tables that are created to be owned by the set role or the direct user.
This should eliminate the need for the reassign-table-owner-postgres.R script because the tables will already be owned by the data manipulator role instead of by the employee user.