Skip to content

Claim Public Schema Ownership

See who owns the public schema of a Managed PostgreSQL database, fix permission denied for schema public, and change the owner of the schema when your tooling needs a named owner.

Who Owns the Public Schema

In every database of a Managed PostgreSQL service, the public schema is owned by pg_database_owner:

SELECT nspname, pg_catalog.pg_get_userbyid(nspowner) AS owner
  FROM pg_catalog.pg_namespace
 WHERE nspname = 'public';

pg_database_owner is a predefined PostgreSQL role whose only member is the owner of the current database. Whoever owns the database therefore has owner rights on its public schema.

defaultdb, and every database created with exo dbaas database create, is owned by avnadmin. In those databases, avnadmin can create, change and drop objects in public without any extra step. Check the owners of the databases with:

SELECT datname, pg_catalog.pg_get_userbyid(datdba) AS owner
  FROM pg_catalog.pg_database
 ORDER BY 1;

The system schemas pg_catalog, information_schema and pg_toast are owned by postgres, a superuser role reserved to the platform. avnadmin is not a member of it.

Fix Permission Denied for Schema Public

Since PostgreSQL 15, the oldest major version the service offers, PUBLIC has USAGE on the public schema but not CREATE. Only the schema owner, and the roles it grants CREATE to, can create objects there. Any other role gets:

ERROR:  permission denied for schema public

This is the usual error for an application whose migrations run as a user other than the database owner, for example after a move from PostgreSQL 13 or 14. Read the access list of the schema:

SELECT nspname, nspacl
  FROM pg_catalog.pg_namespace
 WHERE nspname = 'public';

As avnadmin, connected to the database, grant CREATE to the roles that create objects, typically the migration user:

GRANT USAGE, CREATE ON SCHEMA public TO APP_OWNER;

Parameters:

  • APP_OWNER: the user that runs the migrations.

Tip

Instead of opening public to several applications, give each application its own schema: CREATE SCHEMA app_a AUTHORIZATION APP_OWNER;.

Change the Owner of the Public Schema

Some tools expect a named role, not pg_database_owner, to own public.

As the Database Owner

In a database it owns, avnadmin is the schema owner through pg_database_owner, so it can change the owner directly, to itself or to a role it created:

ALTER SCHEMA public OWNER TO avnadmin;

To go back to the default:

ALTER SCHEMA public OWNER TO pg_database_owner;

As Another User

A user that does not own the database cannot run ALTER SCHEMA public OWNER: the statement fails with must be owner of schema public. The claim_public_schema_ownership() function of the aiven_extras extension makes the user that calls it the owner of public.

  1. As avnadmin, connected to the database, install the extension and allow the user to call the function:

    CREATE EXTENSION IF NOT EXISTS aiven_extras;
    GRANT USAGE ON SCHEMA aiven_extras TO USERNAME;
    GRANT EXECUTE ON FUNCTION aiven_extras.claim_public_schema_ownership() TO USERNAME;
  2. Connected as that user to the same database, claim the schema:

    SELECT aiven_extras.claim_public_schema_ownership();

Parameters:

  • USERNAME: the user that must own public, for example the migration user.

Without the grants, step 2 fails with permission denied for function claim_public_schema_ownership. Check the result with the ownership query from the first section. To give the schema back to pg_database_owner, run ALTER SCHEMA public OWNER TO pg_database_owner; as avnadmin.

Extensions and schema ownership are per database: repeat these steps in each database where you need them. For the other functions of aiven_extras, see Use aiven_extras for Superuser Operations.

Related Pages

Last updated on