This page is a work in progress.
A PostgREST instance exposes all the tables, views, and stored procedures of a single PostgreSQL schema (a namespace of database objects). This means private data or implementation details can go inside different private schemas and be invisible to HTTP clients.
It is recommended that you don’t expose tables on your API schema. Instead expose views and stored procedures which insulate the internal details from the outside world. This allows you to change the internals of your schema and maintain backwards compatibility. It also keeps your code easier to refactor, and provides a natural way to do API versioning.
By default, when a function is created, the privilege to execute it is not restricted by role. The function access is PUBLIC—executable by all roles(more details at PostgreSQL Privileges page). This is not ideal for an API schema. To disable this behavior, you can run the following SQL statement:
ALTER DEFAULT PRIVILEGES IN SCHEMA api REVOKE EXECUTE ON FUNCTIONS FROM PUBLIC;
See PostgreSQL alter default privileges for more details.
After that, you’ll need to grant EXECUTE privileges on functions explicitly:
GRANT EXECUTE ON FUNCTION login TO anonymous; GRANT EXECUTE ON FUNCTION reset_password TO web_user;
A function is executed with the privileges of the user who calls it. This means that the user has to have all permissions to do the operations the procedure performs. If the function accesses private database objects, your API roles won’t be able to succesfully execute the function.
Another option is to define the function with the
SECURITY DEFINER option. Then only one permission check will take place, the permission to call the function, and the operations in the function will have the authority of the user who owns the function itself.
-- login as a user wich has privileges on the private schemas -- create a sample function create or replace function login(email text, pass text) returns jwt_token as $$ begin -- access to a private schema called 'auth' select auth.user_role(email, pass) into _role; -- other operations -- ... end; $$ language plpgsql security definer;
SECURITY DEFINER keywords at the end of the function. See PostgreSQL documentation for more details.
Views are invoked with the privileges of the view owner, much like stored procedures with the
SECURITY DEFINER option. When created by a SUPERUSER role, all row-level security will be bypassed unless a different, non-SUPERUSER owner is specified.
For changing this, we can create a non-SUPERUSER role and make this role the view’s owner.
CREATE ROLE api_views_owner NOINHERIT; ALTER VIEW sample_view OWNER TO api_views_owner;
Insertion on VIEWs with complex RULEs might not work out of the box with PostgREST. It’s recommended that you use triggers instead of RULEs. If you want to keep using RULEs, a workaround is to wrap the VIEW insertion in a stored procedure and call it through the Stored Procedures interface. For more details, see this github issue.