Skip to main content
The CREATE PROCEDURE statement defines a stored procedure.

Required privileges

  • To create a procedure, a user must have CREATE privilege on the schema of the procedure. The user must also have privileges on all the objects referenced in the procedure body.
  • To create a procedure with a user-defined type, a user must have USAGE privilege on the user-defined type.
  • To resolve a procedure, a user must have at least the USAGE privilege on the schema of the procedure.
  • To call a procedure, a user must have EXECUTE privilege on the procedure. By default, the user must also have privileges on all the objects referenced in the procedure body. However, a SECURITY DEFINER procedure executes with the privileges of the user that owns the procedure, not the user that calls it. A SECURITY INVOKER procedure executes with the privileges of the user that calls the procedure, thus matching the default behavior.
For an example of SECURITY DEFINER, refer to Create a SECURITY DEFINER function.
If you grant EXECUTE privilege as a default privilege at the database level, newly created procedures inherit that privilege from the database.

Synopsis

create_proc syntax diagram

Parameters

Examples

The following are examples of basic stored procedures. For a more detailed example of a stored procedure, see Create a stored procedure using PL/pgSQL.

Create a stored procedure that uses a composite-type variable

Create a composite variable:
Create the procedure, declaring the comp variable you created:

Create a stored procedure that uses OUT and INOUT parameters

The following example uses a combination of OUT and INOUT parameters to modify a provided value and output the result. An OUT parameter returns a value, while an INOUT parameter passes an input value and returns a value.
When calling a procedure, you need to supply placeholder values for any OUT parameters. A NULL value is commonly used. When calling a procedure from another routine, you should declare variables that will store the results of the OUT parameters.

Create a stored procedure that calls a procedure

The following example defines a procedure that calls the double_triple example procedure. The triple_result variable is assigned the result of the OUT parameter, while the double_input variable both provides the input and stores the result of the INOUT parameter.
A procedure with OUT parameters can only be called from a PL/pgSQL routine.

Create a stored procedure that uses conditional logic

The following example uses PL/pgSQL conditional statements:

Create a stored procedure that uses a WHILE loop

The following example uses PL/pgSQL loop statements:

Create a LANGUAGE SQL procedure that contains DDL

The following LANGUAGE SQL procedure creates a table when called. A LANGUAGE SQL procedure body can include DDL and DCL statements without late binding. Because a LANGUAGE SQL procedure body is early-bound, its statements cannot reference an object created earlier in the body.

Create a late-bound PL/pgSQL procedure that contains DDL

A PL/pgSQL procedure body that contains DDL requires late binding. Enable the sql.procedures.plpgsql.late_binding.enabled cluster setting:
Create a role for the procedure to grant privileges to:
The following procedure creates a schema and a table, grants privileges on the table, and inserts a row. Because the procedure uses late binding, the GRANT and INSERT statements can reference the table created earlier in the procedure body.

See also