Re: CREATE USER from within function - Mailing list pgsql-general

From Stephan Szabo
Subject Re: CREATE USER from within function
Date
Msg-id 20030409075416.S65184-100000@megazone23.bigpanda.com
Whole thread Raw
In response to CREATE USER from within function  (Michal Taborsky <michal@taborsky.cz>)
List pgsql-general
On Wed, 9 Apr 2003, Michal Taborsky wrote:

> I am facing a problem. I am unable to call CREATE USER from within a
> PL/PgSQL function. I am using Postgres 7.3.2.
>
> The function looks like this (it is more complex in fact, but even if
> stripped to this, it does not work anyway):
>
> CREATE OR REPLACE FUNCTION
> usr_createAccount(integer, name, text, text, bool, bool)
> RETURNS bool AS '
> BEGIN
> CREATE USER $2 WITH PASSWORD $3 NOCREATEDB CREATEUSER IN GROUP "all",
> "super";
> RETURN true;
> END;
> ' LANGUAGE 'plpgsql';
>
> I keep getting this:
>
> akcent=# select usr_createAccount(3, 'username', 'somepassword', 'cs',
> true, false);
> WARNING:  Error occurred while executing PL/pgSQL function usr_createaccount
> WARNING:  line 2 at SQL statement
> ERROR:  parser: parse error at or near "$1" at character 14
>
> I am a bit confused by this error, because there is no "$1" string in
> this function. I tried to do a CREATE USER in transaction, because I
> suspected it to be the problem, but it worked just fine. Does anyone see
> something which I don't ? What am I doing wrong ?

I think it's getting confused by USER which is also a reserved word to
return the current user.  You can use EXECUTE to do this however (as a
note, you'll also need single quotes around the password afaict).
Something like:

CREATE OR REPLACE FUNCTION
usr_createAccount(integer, name, text, text, bool, bool)
RETURNS bool AS '
BEGIN
EXECUTE ''CREATE USER '' || $2 || '' WITH PASSWORD '''''' || $3 || ''''''
NOCREATEDB CREATEUSER IN GROUP "all", "super";'';
 return true;
end;' language 'plpgsql';


pgsql-general by date:

Previous
From: "scott.marlowe"
Date:
Subject: Re: Cross database reference (databases are on the same
Next
From: Stephan Szabo
Date:
Subject: Re: multiple fields index