X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id B9A5252A73 for ; Tue, 19 Apr 2005 00:57:31 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 24421-02 for ; Tue, 19 Apr 2005 03:57:24 +0000 (GMT) Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by svr1.postgresql.org (Postfix) with ESMTP id B9B39531E0 for ; Tue, 19 Apr 2005 00:57:21 -0300 (ADT) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.3/8.13.3) with ESMTP id j3J3vDNm030288 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 18 Apr 2005 21:57:16 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.3/8.13.3) with ESMTP id j3J3vDdN076084; Mon, 18 Apr 2005 21:57:13 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.3/8.13.3/Submit) id j3J3vDIO076083; Mon, 18 Apr 2005 21:57:13 -0600 (MDT) (envelope-from mfuhr) Date: Mon, 18 Apr 2005 21:57:13 -0600 From: Michael Fuhr To: shaun Cc: pgsql-sql@postgresql.org Subject: Re: CREATE USER in side a TRIGGER FUNCTION Message-ID: <20050419035713.GA76019@winnie.fuhr.org> References: <425F353A.6060700@mozapps.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <425F353A.6060700@mozapps.org> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.004 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200504/273 X-Sequence-Number: 21166 On Thu, Apr 14, 2005 at 08:30:02PM -0700, shaun wrote: > > I want to put login and user managment into the database for security > reasons. I have a employee table. When I add a person to the employee > table I want to create them in the database and when I remove a person I > want to drop the person from the database also. How do you do it and > what is wrong the the following code? > > CREATE FUNCTION insuser () RETURNS TRIGGER AS > 'BEGIN > IF NEW.role = ''clerk'' THEN > EXECUTE CREATE USER NEW.login IN GROUP gp_clerk; EXECUTE needs a string. Try something like this: EXECUTE ''CREATE USER '' || quote_ident(NEW.login) || '' IN GROUP gp_clerk''; quote_ident() is a safety measure to prevent SQL injection. For more information about using EXECUTE, see "Executing Dynamic Commands" in the PL/pgSQL documentation (if you're using a version of PostgreSQL earlier than 8.0 then see the documentation for that version): http://www.postgresql.org/docs/8.0/interactive/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN -- Michael Fuhr http://www.fuhr.org/~mfuhr/