473,396 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem in function with CREATE ROLE with parameters

hi to everyone,
I tried to create a function that includes a role creation with the rolename and password as parameters to be called by other function but the postgresql raises an error like that:

ERROR: syntax error at or near "$1"
SQL state: 42601
Character: 81

The function definition is:
CREATE FUNCTION new_user (text,text) RETURNS integer AS $$
CREATE ROLE $1 PASSWORD $2;
$$ language sql;


I'm using the right syntax?
I tried also with pl/pgSQL giving names to the parameters like myname and mypassword with the same results. I'm using PostgreSQL 8.2 with winxp.
Please Help!!
Thanks in advance and happy Easter!
Apr 7 '07 #1
3 6408
michaelb
534 Expert 512MB
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)

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. CREATE or replace FUNCTION new_user (text, text) RETURNS text AS '
  4. BEGIN
  5.   execute ''CREATE USER '' || quote_ident($1) || '' PASSWORD '' || quote_literal($2);
  6.   return  ''Created user '' || $1 || '' with password '' || $2;
  7. END;
  8. ' language plpgsql;
  9.  
  10.  
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:
Expand|Select|Wrap|Line Numbers
  1. testdb=# select new_user('simon', 'rio25');
  2.                 new_user                
  3. ----------------------------------------
  4.  Created user simon with password rio25
  5. (1 row)
  6.  
  7.  
You can check whether plpgsql is available on your database with this query:
Expand|Select|Wrap|Line Numbers
  1. testdb=# select lanname from pg_language;
  2.  lanname  
  3. ----------
  4.  plpgsql
  5.  sql
  6.  internal
  7.  c
  8. (4 rows)
  9.  
If it's not there you need to install it, see createlang
and CREATE LANGUAGE for details
Apr 7 '07 #2
Hi Michaelb,

thanks for your welcome and your very fast response.

I used it and it worked well!

Thank you very much!
Apr 7 '07 #3
michaelb
534 Expert 512MB
Hi Michaelb,

thanks for your welcome and your very fast response.
I used it and it worked well!
Thank you very much!
You're very welcome!
Apr 8 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: JKop | last post by:
Take the following simple function: unsigned long Plus5Percent(unsigned long input) { return ( input + input / 20 ); } Do yous ever consider the possibly more efficent:
4
by: Viktor Popov | last post by:
Hi , I have the following SP: CREATE PROCEDURE prSVUSERINFO @USRNAM VARCHAR(20), @USRPSW VARCHAR(20), @NAME VARCHAR(64), @ADDR VARCHAR(74), @EML VARCHAR(64), @ROLE VARCHAR(6), @RCOUNT INT...
1
by: Thanks | last post by:
I have a routine that is called on Page_Init. It retrieves folder records from a database which I display as Link Buttons in a table cell. I set the table cell's bgcolor to a default color (say...
1
by: Eustice Scrubb | last post by:
I'm trying to use the Insert.aspx Quickstart and I'm getting a NULL pointer exception. Any help? <code> <script language="VB" runat="server"> Dim myConnection As SqlConnection Sub...
0
by: CJM | last post by:
Repeated for the benefit of m.p.i.asp.general, which I forgot to include in the original posting... "CJM" <cjmnews04@REMOVEMEyahoo.co.ukwrote in message news:4lo3f8F2shqtU1@individual.net...
2
by: MS News Public | last post by:
Hi I have an asp.net 2.0 project and am experiencing a problem. In the project, I am trying to make use of Membership. I have one Role, called "Basic User" and two users - "admin" and...
5
by: tom.hepworth | last post by:
Hi I have a problem which I hope someone can help me with because I really don't even know where to start with it. I am using Access 2003. I have a delimited text file which contains about...
21
by: adjo | last post by:
I am working on an app with an Access2002 frontend and Sql2005 backend. I have to use integrated security. I want to prevent my users from altering data in another way than via the frontend. It...
2
by: priyamtheone | last post by:
I'm trying to create a stored procedure in MSSQL Server 2005 that'll perform the following jobs: 1) Create a login. 2) Create an user in TestDB database for the login created in step 1. 3) Assign...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.