Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make Postgres extension available to non superuser

I installed a Postgres extension (unaccent) with

sudo su posgres
psql create extension unaccent

and now I can use unacccent in sql, but only if I am the Postgres user.

How do I make Postgres extension available to all/another user

(Im on Ubuntu using Postgres 9.3.5 installed using apt-install)

jthinksearch=# \dx;
                         List of installed extensions
   Name   | Version |   Schema   |                 Description
----------+---------+------------+---------------------------------------------
 plpgsql  | 1.0     | pg_catalog | PL/pgSQL procedural language
 unaccent | 1.0     | public     | text search dictionary that removes accents
(2 rows)

jthinksearch=#


jthinksearch=> \du;
                             List of roles
 Role name |                   Attributes                   | Member of
-----------+------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication | {}
 ubuntu    |                                                | {}

postgres@ip-172-31-39-147:/home/ubuntu/code/jthinksearch/reports/src/main/sql$ exit ubuntu@ip-172-31-39-147:~/code/jthinksearch/reports/src/main/sql$ psql jthinksearch psql (9.3.5) Type "help" for help.

I gave user superuser role but that didnt help, then as suggested put the schema name in , that had an effect on the error message but still didnt work

jthinksearch=# \du;
                             List of roles
 Role name |                   Attributes                   | Member of
-----------+------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication | {}
 ubuntu    | Superuser                                      | {}

jthinksearch=# select unaccent(name) from musicbrainz.artist where id=195660;
ERROR:  function unaccent(character varying) does not exist
LINE 1: select unaccent(name) from musicbrainz.artist where id=19566...
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
jthinksearch=# ^C
jthinksearch=# select public.unaccent(name) from musicbrainz.artist where id=195660;
ERROR:  text search dictionary "unaccent" does not exist
jthinksearch=#
like image 574
Paul Taylor Avatar asked Jun 12 '15 10:06

Paul Taylor


People also ask

How do I give superuser permissions in PostgreSQL?

Log into PostgreSQL and run the following ALTER USER command to change user test_user to superuser. Replace test_user with username as per your requirement. postgres-# ALTER USER test_user WITH SUPERUSER; In the above command, we use WITH SUPERUSER clause to change user to superuser.

What is the extension of PostgreSQL files?

Each PSQL database table is a separate file with a default file extension of . mkd.

How do I add extensions to pgadmin4?

Click the Definition tab to continue. Use the Definition tab to select the Schema and Version: Use the drop-down listbox next to Schema to select the name of the schema in which to install the extension's objects. Use the drop-down listbox next to Version to select the version of the extension to install.

How do you change role in PostgreSQL?

Syntax: ALTER ROLE role_name [WITH] option; The following functions are available with ALTER ROLE statement: SUPERUSER | NOSUPERUSER – It is used to determine if the role is a superuser. VALID UNTIL 'timestamp' – It is used to specify the expiry date and time of a role's password.


Video Answer


1 Answers

Based on this error message:

ERROR: text search dictionary "unaccent" does not exist

and the previous one where unaccent without the schema prefix is not found, it means that the public schema, where the unaccent function resides, is not in your search_path.

It happens that unaccent fails in this case because it's a dictionary function and basically it needs to find its stuff through the search_path.

This is explained in more details in Does PostgreSQL support “accent insensitive” collations?

Once the public schema is added to the search_path of the users who need to call it (this is normally the default), this should work and they don't need to be superuser.

Or if this solution is not acceptable, you may also use an intermediate stub function that embeds the schema and adds immutability, as suggested in the answer linked above.

like image 98
Daniel Vérité Avatar answered Sep 19 '22 08:09

Daniel Vérité