BETASDKs MIT, CLI and services Apache 2.0, schema engine BSL 1.1. Cloud is a closed beta: create an account to request access.
POSTGRES / POSTGRES WITHOUT SUPERUSER

Superuser features, without the superuser

pg_guard lets a non-superuser role create extensions, event triggers and publications. It still cannot make itself a superuser.

pg_guard.sql
-- postgres is not a superuser here. It is a member of
-- supatype_privileged_role, the image's pg_guard.privileged_role.
set role postgres;
select current_setting('is_superuser');  -- off

create extension hstore;                 -- in pg_guard.privileged_extensions
create publication app_changes for all tables;
set session_replication_role = replica;  -- in privileged_role_allowed_configs

-- An event trigger. The function must not be superuser-owned.
create function log_ddl() returns event_trigger
language plpgsql as $$
begin
  raise notice 'ddl by %', current_user;
end;
$$;
create event trigger log_ddl on ddl_command_end
  execute procedure log_ddl();

create role escalate superuser;
-- ERROR:  permission denied to create role
THE PROBLEM

Why pg_guard

Postgres keeps a lot behind SUPERUSER. Event triggers, publications, foreign data wrappers and extensions such as hstore all need it. So teams hand out SUPERUSER, and with it the power to read server files, run programs and change any role.

pg_guard adds one privileged role. When that role creates a superuser-only object, pg_guard switches to a configured superuser, creates it, hands ownership back and switches back. Event triggers it creates skip superusers and reserved roles, so they cannot be used to escalate. It cannot create a SUPERUSER role or grant the attribute.

Everything lives in postgresql.conf. There are no tables, functions or security labels in your database, so upgrades are simple and a reload applies a change. It is derived from supautils and runs in every supatype/postgres container.

COMPARED

pg_guard or handing out SUPERUSER

handing out SUPERUSERpg_guard
Extensions that need a superuserOnly a superuser can create hstore, postgres_fdw and the like.Non-superusers can create, update and drop any extension listed in pg_guard.privileged_extensions. Others behave as normal.
Event triggersSuperuser only. A trigger fires for every role, superusers included.The privileged role can create them. They are skipped for superusers and reserved roles.
Publications and FDWsSuperuser only.The privileged role creates them and owns them.
Becoming a superuserThe role already is one.The privileged role cannot create a SUPERUSER role or add the attribute to one.
Roles your services log in asAny superuser can alter or drop them, or grant pg_read_server_files.Reserved roles cannot be altered or dropped by non-superusers. Reserved memberships cannot be granted.
Where the rules liveRole attributes and grants, set role by role.postgresql.conf only. Settings take effect on a reload, with no objects added to the database.
LIMITS

What it does not do

  • Event triggers made by the privileged role do not fire when it creates publications, foreign data wrappers or extensions.
  • Those event triggers are skipped for superusers and reserved roles, so they do not see DDL from those roles.
  • It does not restrict superusers. Keep the real superuser for the platform, not for people.
  • It loads per session through session_preload_libraries. A role given its own list without pg_guard runs without it. supatype migrate doctor checks this for authenticator.
  • Extension parameter overrides support the schema parameter only. Custom scripts run before-create and after-create only.
  • Settings are cluster-wide. You cannot give one database a different list.
  • It is not in the cross-compiled linux-arm64 native archive. The Docker image is not affected.
NEXT STEPS

Two ways to run it

On its own

pg_guard ships in the supatype/postgres image. Use the image as your Postgres, with or without the rest of Supatype.

terminal
# pg_guard is on by default in the image
docker run -d --name supatype-postgres \
  -e POSTGRES_PASSWORD=your-password -p 5432:5432 \
  supatype/postgres:17-latest

# Preloaded for every session, with its settings in their own file:
#   session_preload_libraries = 'pg_guard'
#   include = '/etc/postgresql-custom/pg_guard.conf'
docker exec supatype-postgres \
  cat /etc/postgresql-custom/pg_guard.conf

# Make one of your roles privileged (as supatype_admin)
psql -h localhost -U supatype_admin -d postgres \
  -c "grant supatype_privileged_role to app_admin;"

# After editing pg_guard.conf, reload. No restart needed.
psql -h localhost -U supatype_admin -d postgres \
  -c "select pg_reload_conf();"
With Supatype

Get the image already running, with migrations, access rules, an API, a typed client and Studio generated from your TypeScript types. Cloud starts with 2 projects, 500 MB database and 1 GB storage, free and with no card.