Run a Function in PostgreSQL: Commands and Examples

Formulas & functions · Updated November 22, 2026

To run a PostgreSQL function, call it in a SQL statement—usually with SELECT, as in SELECT public.add_numbers(4, 7);. Replace the schema, function name, and arguments with yours. You can run that statement in psql, pgAdmin’s Query Tool, or another PostgreSQL client. Before you begin, find the function’s argument types and return type; those determine the right call and how to read its result.

What You’ll Need

  • Access to a PostgreSQL database through psql, pgAdmin, or another SQL client.
  • A database role with permission to connect and, if needed, permission to execute the function.
  • The function’s name, schema, required arguments, and argument types. If you do not know these, inspect the function before calling it.

Step-by-Step

  1. Connect to the right database. Open your SQL client and connect to the database that contains the function. In psql, you can connect at startup with psql -h hostname -U username -d database_name, substituting your server, login, and database details. If psql is already open, use \conninfo to check the current connection. In pgAdmin, select the relevant server and database, then open Tools > Query Tool. Running a valid call against the wrong database can still produce a function-not-found error.
  2. Find the function and its signature. In psql, list functions with \df; narrow the list with a pattern such as \df public.add_numbers. Use \df+ for additional details, including the function definition where available. In pgAdmin, expand the database’s Schemas and then the schema’s Functions node to locate a function and inspect its properties. Confirm the schema, argument count and types, default values, and return type. Overloaded functions can share a name but accept different argument types.
  3. Call a scalar function with SELECT. A scalar function returns one value, such as a number, date, or text. Call it in a query and end the statement with a semicolon. For example, the following creates a small SQL function and runs it. Creating it is optional if the function already exists; use an appropriate name and schema for your own database.
    CREATE OR REPLACE FUNCTION public.add_numbers(a integer, b integer)
    RETURNS integer
    LANGUAGE sql
    IMMUTABLE
    AS $$
      SELECT a + b;
    $$;
    
    SELECT public.add_numbers(4, 7);

    The query returns a single column containing 11. The function definition shows two integer parameters and an integer return type. The call supplies two integer values in the same order as the parameters. You need permission to create or replace a function to run the definition; executing an existing function does not require creating it.

  4. Supply arguments in the correct form. Positional arguments go in the order listed in the signature. Use single quotes around text and date values, and use NULL without quotes when you mean a missing value. PostgreSQL can convert some compatible values automatically, but do not depend on implicit conversions when types are ambiguous. You can specify a type explicitly, for example SELECT public.add_numbers(4::integer, 7::integer);. To avoid positional confusion, use named notation when you know the parameter names: SELECT public.some_function(customer_id => 42, include_closed => false);. Substitute the actual function and parameter names; named arguments let you provide arguments in a different order.
  5. Use the schema and handle the return type. Writing the schema, such as public.function_name(...), removes uncertainty about which function PostgreSQL should call. For a scalar result, SELECT displays the returned value. A function that returns multiple rows is usually called in the FROM clause. For example, if a function returns a set of order rows, use SELECT * FROM public.orders_for_customer(42);. If it returns a table with known columns, name those columns instead of using * when you only need some of them. Review the declared return type to choose the right query shape.
  6. Run the statement and check the output. In psql, paste the call at the prompt and press Enter; a semicolon tells psql the SQL statement is complete. In pgAdmin’s Query Tool, enter the statement and select Execute/Refresh (the play button) or press F5. Check the results grid or psql output, including whether the result is one value, several rows, or no rows. If the function performs a permitted side effect, such as inserting a record, check the affected data as well. Do not assume that a blank-looking result means the call failed: a function returning void has no useful value to display.
  7. Decide whether to keep changes. If the function changes data, understand the transaction behavior of your client. In an explicit transaction, finish with COMMIT; to keep changes or ROLLBACK; to discard them. In psql, autocommit is normally on unless you have opened a transaction with BEGIN;. pgAdmin settings and client tools can affect transaction handling, so check whether a transaction is open before closing the session. A function is not the same as a procedure: use SELECT to invoke a function, but use CALL procedure_name(...); to invoke a procedure. Procedures can support transaction control in contexts where functions cannot.

Common Mistakes

Using CALL for a function. PostgreSQL distinguishes functions from procedures. A function is generally invoked in a SQL expression, commonly with SELECT; CALL is for procedures. Check the object type if you are unsure.

Leaving out an argument or passing the wrong type. Compare your call with the function signature. Optional parameters may have defaults, but required ones must be supplied. A text value such as '42' is not always interchangeable with the integer value 42, especially when PostgreSQL must choose among overloaded functions.

Calling the wrong schema or database. A function may exist under a schema other than the one in your search path, or in another database entirely. Qualify the function with its schema and confirm your connection before changing the call.

Expecting a row when the function returns void or no rows. Read the return type and, for set-returning functions, distinguish between a successful empty result and an error. Check the function’s logic and input values if it returns no rows.

Tips

Use schema-qualified names in scripts that might run in different environments. That makes the intended function explicit and reduces surprises caused by a changed search path. Keep a copy of the exact signature beside a complicated call, especially when functions are overloaded or have several optional parameters.

Test a function that changes data inside a transaction when that is appropriate for your workflow: start with BEGIN;, run the call, inspect the results, then choose COMMIT; or ROLLBACK;. This is not a substitute for a backup or a review of the function’s effects, but it can help you avoid keeping an unintended change. For repeatable work, save the call in a SQL file and record which database it is intended for.

Troubleshooting

Function does not exist. Check the database connection, spelling, capitalization, schema, and argument types. Unquoted PostgreSQL identifiers are folded to lowercase, so a function created with quoted mixed-case lettering must be called with matching double quotes. In psql, run \df function_name or \df schema_name.function_name to look for a matching signature. If the function is in another database, connect to that database; PostgreSQL does not let a normal function call cross databases just by qualifying the name.

More than one function matches. This usually indicates overloaded functions or arguments whose types are unclear. Check the available signatures, then add explicit casts such as ::integer or use named arguments where appropriate. Make sure the cast matches a real parameter type rather than forcing a value into an unintended type.

Permission denied. Ask a database administrator or the function owner to check your access. PostgreSQL function execution privileges can be granted with GRANT EXECUTE ON FUNCTION schema_name.function_name(argument_types) TO role_name;. The argument types in the grant identify the specific function signature. Do not request broad privileges if access to one function is sufficient.

The query runs but the result is unexpected. Verify the inputs, data types, and function’s return type, then inspect the function definition if you have permission. A function may return NULL, no rows, or a value shaped differently from what the client displays. If an error occurs, read the full PostgreSQL message: its detail and hint often identify the mismatched type, missing object, or failing statement.

Frequently Asked Questions

How do I run a PostgreSQL function with no arguments?
Use empty parentheses, for example SELECT public.current_status();. Parentheses make clear that you are calling the function. Include the schema if you want to identify the function unambiguously.

Can I run a function from psql?
Yes. Connect to the target database, enter a SQL call such as SELECT public.add_numbers(4, 7);, and finish it with a semicolon. psql prints the result in a text table. Use \q to exit when finished.

Can I run a PostgreSQL function in pgAdmin?
Yes. Open the correct database’s Query Tool, enter the call, and execute it. The results appear in the output grid. You can also browse the Functions node to inspect a function, but you still invoke it with SQL such as SELECT.

What is the difference between SELECT and CALL?
SELECT invokes a function and can display its returned value or rows. CALL invokes a procedure, which is a different PostgreSQL object type. If you do not know which you have, inspect the object in your client or check its definition and return type.

How do I run a function that returns several rows?
Use it as a table source, typically with SELECT and FROM, for example SELECT * FROM public.orders_for_customer(42);. Replace the example with your function and arguments, and select the returned columns you need.

Do I need to create the function before running it?
No, if it already exists in the connected database and you have permission to execute it. You need the appropriate privileges only if you are creating or replacing its definition. Confirm the function’s schema and signature before calling an existing one.

Related

Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.

More in Formulas & functions

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.