Alternatively, starting with Postgresql 13, a role having the CREATE
privilege on the current database can install trusted extensions without being a SUPERUSER
.
From Postgresql documentation :
Loading an extension ordinarily requires the same privileges that would be required to create its component objects. For many extensions this means superuser privileges are needed.
However, if the extension is marked trusted in its control file, then it can be installed by any user who has CREATE
privilege on the current database.
In this case the extension object itself will be owned by the calling user, but the contained objects will be owned by the bootstrap superuser (unless the extension's script explicitly assigns them to the calling user). This configuration gives the calling user the right to drop the extension, but not to modify individual objects within it.
Depending on your installation, trusted extensions should at least be the following :
btree_gin
btree_gist
citext
cube
dict_int
fuzzystrmatch
hstore
intarray
isn
lo
ltree
pgcrypto
pg_trgm
plpgsql
seg
tablefunc
tcn
tsm_system_rows
tsm_system_time
unaccent
uuid-ossp
You can check or switch the trusted
flag in the /usr/local/share/postgresql/extension/*.control
files, again depending on your installation.
Giving a PostgreSQL Instance with a database maindb
and two roles:
postgres
(default superuser)
jack
(limited privileges)
$ psql -U jack -d maindb
maindb=>CREATE EXTENSION plpgsql;
ERROR: permission denied to create extension "plpgsql"
HINT: Must have CREATE privilege on current database to create this extension.
maindb=> \q
Obviously jack
can't add the extension to maindb
, now let's give them the right privilege and try again :
$ psql -U postgres
postgres=# GRANT CREATE ON DATABASE maindb to jack;
GRANT
postgres=# \q
$ psql -U jack -d maindb
maindb=>CREATE EXTENSION plpgsql;
CREATE EXTENSION
And voilà, no need to give the SUPERUSER
privileges anymore (which you should never do anyway).