Re: Use "CREATE USER" in plpgsql function

From: "Igor Neyman" <ineyman(at)perceptron(dot)com>
To: "Tatarnikov Alexander" <cankrus(at)gmail(dot)com>, <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Use "CREATE USER" in plpgsql function
Date: 2010-09-15 14:26:48
Message-ID: F4C27E77F7A33E4CA98C19A9DC6722A206827C5F@EXCHANGE.corp.perceptron.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

> -----Original Message-----
> From: Tatarnikov Alexander [mailto:cankrus(at)gmail(dot)com]
> Sent: Wednesday, September 15, 2010 12:05 AM
> To: pgsql-sql(at)postgresql(dot)org
> Subject: Use "CREATE USER" in plpgsql function
>
> Hello!
>
> I have function wich check user credentials and if test
> passed function must create new user with generated username
> and password.
>
> Language is plpgsql.
>
> For example:
>
> ....
> DECLARE creds RECORD;
> ...
> SELECT * INTO creds FROM ...
>
> creds is Record with fields userName(VARCHAR) and
> userPassword(VARCHAR)
>
> so when i use CREATE USER creds."userName" WITH PASSWORD
> creds."userPassword"
>
> i get an error, because creds."userName" is VARCHAR and thus
> when function runs it will be look like this:
> CREATE USER 'user_1'
> but right command is
> "CREATE USER user_1" OR " CREATE USER "user_1" "
>
> so question is how to "unembrace" this parameter (i mean
> creds."userName")?
>
> Thanks
> --
> ------
> Alexander
>

It is called "dynamic" sql:

EXECUTE 'CREATE USER creds.' || userName || ' WITH PASSWORD creds.' ||
userPassword;

Read about "dynamic" sql in PG docs:

http://www.postgresql.org/docs/8.4/interactive/plpgsql-statements.html

Specifically: "38.5.4. Executing Dynamic Commands"

Regards,
Igor Neyman

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Asko Oja 2010-09-15 18:28:40 Re: Use "CREATE USER" in plpgsql function
Previous Message Sergey Konoplev 2010-09-15 07:55:03 Re: Use "CREATE USER" in plpgsql function