Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

174
Vistas
Create role programmatically with parameters

I'm looking for a way to create a function that takes in two parameters for user name and password and creates a read only role with it. I've tried something like:

create or replace function create_user_readonly (
  unm varchar,
  pwd varchar
)
  returns varchar(10) as $$

begin

  create role unm login password @pwd;
  return 'success';

end;

$$ language plpgsql;

This throws the error:

[42601] ERROR: syntax error at or near "@" Position: 151

I thought of using dynamic SQL to construct the query but ran into this here (https://www.postgresql.org/docs/9.1/static/plpgsql-statements.html):

Another restriction on parameter symbols is that they only work in SELECT, INSERT, UPDATE, and DELETE commands. In other statement types (generically called utility statements), you must insert values textually even if they are just data values.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

here is an example:

create or replace function create_user_readonly (
  unm varchar,
  pwd varchar
)
  returns varchar(10) as $$

begin

  execute format($f$create role %I login password '%s'$f$,unm,pwd);
  execute format('alter role %I set transaction_read_only to on',unm);
  return 'success';

end;

$$ language plpgsql;

keep in mind though you will need to alter user to set transaction_read_only also to make it read only. also:

CREATE ROLE does not offer setting RO default to role.

ALTER ROLE does

And keep in mind that overcoming uset configuration transaction_read_only is as easy as running one statement.

and create role won't give CONNECT permission, use CREATE USER instead if you want one.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda