Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installing PostgreSQL Extension to all schemas

Tags:

postgresql

I'm on PostgresQL 9.1.1 trying to have the extension unaccent available on all schemas.

So I ran the command CREATE EXTENSION unaccent;. Which works, but only for the current schema set on search_path. So this means if I change the search_path, I no longer can call unaccent. How do I make this extension available to all schemas in a particular database?

Thanks in advance!

like image 685
Clash Avatar asked Oct 20 '12 07:10

Clash


People also ask

Where does Postgres install extensions?

Extension Files The file format must be extension_name. control, which tells the basics about extension to PostgreSQL, and must be placed in the installation's SHAREDIR/extension directory.

How do I see all Postgres extensions?

Get a list of all the extensions installed on a database by using the \dx command. For example, the output for \dx when run on the Databases for PostgreSQL default database shows the only installed extension.

What is PostgreSQL file extension?

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


2 Answers

CREATE EXTENSION unaccent; installs the extension into the public schema. To make it usably, simply include that when changing the search_path:

set search_path = my_schema, public; 

Or better create a schema to contain all extensions, then always append that schema to the search_path.

create schema extensions;  -- make sure everybody can use everything in the extensions schema grant usage on schema extensions to public; grant execute on all functions in schema extensions to public;  -- include future extensions alter default privileges in schema extensions    grant execute on functions to public;  alter default privileges in schema extensions    grant usage on types to public; 

Now install the extension:

create extension unaccent schema extensions; 

Then use include that schema in the search_path

set search_path = my_schema, extensions; 

If you don't want to repeat the above for every new database you create, run the above steps while being connected to the template1 database. You can even include the extensions schema in the default search_path by either editing postgresql.conf or using alter system

like image 172
a_horse_with_no_name Avatar answered Sep 25 '22 17:09

a_horse_with_no_name


Had same question, but @Richard Huxton answer led to correct solution:

create extension unaccent schema pg_catalog; 

This works!!

As Richard said, pg_catalog is automatically added (silently) to each search_path. Extensions added there will be found.

imho this is much better than schema.func() if the extension is global.

For example, I use a lot of schemae. I use the schema PUBLIC for debugging - everything should be in its own schema. If something is in PUBLIC, it's wrong.

Creating the extension in pg_catalog keeps all the schema clean, and lets the schema itself work as if it were part of the core postgres.

like image 30
cc young Avatar answered Sep 21 '22 17:09

cc young