Skip to main content
New in v23.2: The CREATE PROCEDURE statement defines a stored procedure.

Required privileges

  • To define a procedure, a user must have CREATE privilege on the schema of the procedure.
  • To define 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.
  • At procedure definition and execution time, a user must have privileges on all the objects referenced in the procedure body. Privileges on referenced objects can be revoked and later procedure calls can fail due to lack of permission.
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 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:

See also