Welcome to the Forum!
SQL state 42601 indicates a syntax error.
You may want to review the release notes, but from what I remember in the previous versions Postgresql utility functions (CREATE USER is one of them) did not take parameter symbols, meaning you have to pass literal constants to CREATE USER.
You can probably verify this by hard-coding name and password inside the function, and you may also need to change the return type to VOID - your code does not seem to return anything.
You can overcome this restriction by using EXECUTE from plpgsql function
as shown here (you can modify the return type as you need)
-
-
-
CREATE or replace FUNCTION new_user (text, text) RETURNS text AS '
-
BEGIN
-
execute ''CREATE USER '' || quote_ident($1) || '' PASSWORD '' || quote_literal($2);
-
return ''Created user '' || $1 || '' with password '' || $2;
-
END;
-
' language plpgsql;
-
-
note that i'm using the old-style syntax, but with your database you can/should use $$ as you did in your code.
give it a test run:
-
testdb=# select new_user('simon', 'rio25');
-
new_user
-
----------------------------------------
-
Created user simon with password rio25
-
(1 row)
-
-
You can check whether plpgsql is available on your database with this query:
-
testdb=# select lanname from pg_language;
-
lanname
-
----------
-
plpgsql
-
sql
-
internal
-
c
-
(4 rows)
-
If it's not there you need to install it, see
createlang
and
CREATE LANGUAGE for details