I want to create a SQL function that can perform the following task - It should check whether a schema exists and if it doesn't, the function should create one.
Here is an example of such a function -
CREATE OR REPLACE FUNCTION create_schema_if_not_exists() RETURNS void AS $body$
DECLARE schema_count INT;
BEGIN
SELECT count(*) into schema_count FROM information_schema.schemata WHERE schema_name = 'my_schema';
if schema_count = 0 THEN
CREATE SCHEMA my_schema;
END IF;
END;
$body$
language 'plpgsql';
Now there are two different kinds of users which this function can have - (1) Permitted to create schema, and (2) Not permitted to create schema. I want this function to error out only when a user, not permitted to creating schema, tries to create a schema when one does not exists.
The problem with this approach is that the user of kind (2) is not able to call function create_schema_if_not_exists
.
How should I approach this problem now?
Edit
So here is what I want -
- If a user of kind (2) tries to create schema when one already exists then nothing should happen.
- If a user of kind (1) tries to create schema when one already exists then nothing should happen.
- If a user of kind (1) tries to create schema when one doesn't already exists then the schema should be created.
- If a user of kind (2) tries to create schema when one doesn't already exists then permission error should be thrown.