diff --git a/README.md b/README.md index 47345c619d..52b4fd5e4b 100644 --- a/README.md +++ b/README.md @@ -666,7 +666,7 @@ Installs the packages containing the development libraries for PostgreSQL and sy ##### `link_pg_config` -If the bin directory used by the PostgreSQL page is not `/usr/bin` or `/usr/local/bin`, symlinks `pg_config` from the package's bin dir into `usr/bin` (not applicable to Debian systems). Set to `false` to disable this behavior. +If the bin directory used by the PostgreSQL page is not `/usr/bin` or `/usr/local/bin`, symlinks `pg_config` from the package's bin dir into `usr/bin` (not applicable to Debian systems). Set to `false` to disable this behavior. Valid values: `true`, `false`. @@ -912,7 +912,7 @@ Default value: OS dependent. ##### `port` -Specifies the port for the PostgreSQL server to listen on. **Note:** The same port number is used for all IP addresses the server listens on. Also, for Red Hat systems and early Debian systems, changing the port causes the server to come to a full stop before being able to make the change. +Specifies the port for the PostgreSQL server to listen on. **Note:** The same port number is used for all IP addresses the server listens on. Also, for Red Hat systems and early Debian systems, changing the port causes the server to come to a full stop before being able to make the change. Default value: 5432. Meaning the Postgres server listens on TCP port 5432. @@ -924,55 +924,55 @@ Default value: `undef`. ##### `postgresql_conf_path` -Specifies the path to your `postgresql.conf` file. +Specifies the path to your `postgresql.conf` file. Default value: '${confdir}/postgresql.conf'. ##### `psql_path` -Specifies the path to the `psql` command. +Specifies the path to the `psql` command. Default value: OS dependent. ##### `service_manage` -Defines whether or not Puppet should manage the service. +Defines whether or not Puppet should manage the service. Default value: `true`. ##### `service_name` -Overrides the default PostgreSQL service name. +Overrides the default PostgreSQL service name. Default value: OS dependent. ##### `service_provider` -Overrides the default PostgreSQL service provider. +Overrides the default PostgreSQL service provider. Default value: `undef`. ##### `service_reload` -Overrides the default reload command for your PostgreSQL service. +Overrides the default reload command for your PostgreSQL service. Default value: OS dependent. ##### `service_restart_on_change` -Overrides the default behavior to restart your PostgreSQL service when a config entry has been changed that requires a service restart to become active. +Overrides the default behavior to restart your PostgreSQL service when a config entry has been changed that requires a service restart to become active. Default value: `true`. ##### `service_status` -Overrides the default status check command for your PostgreSQL service. +Overrides the default status check command for your PostgreSQL service. Default value: OS dependent. ##### `user` -Overrides the default PostgreSQL super user and owner of PostgreSQL related files in the file system. +Overrides the default PostgreSQL super user and owner of PostgreSQL related files in the file system. Default value: 'postgres'. @@ -1020,7 +1020,7 @@ postgresql::server::config_entry { 'check_function_bodies': ##### `ensure` -Removes an entry if set to 'absent'. +Removes an entry if set to 'absent'. Valid values: 'present', 'absent'. @@ -1040,43 +1040,43 @@ Defines a comment to be stored about the database using the PostgreSQL COMMENT c ##### `connect_settings` -Specifies a hash of environment variables used when connecting to a remote server. +Specifies a hash of environment variables used when connecting to a remote server. Default value: Connects to the local Postgres instance. ##### `dbname` -Sets the name of the database to be created. +Sets the name of the database to be created. Default value: the namevar. ##### `encoding` -Overrides the character set during creation of the database. +Overrides the character set during creation of the database. Default value: The default defined during installation. ##### `grant` -Specifies the permissions to grant during creation. +Specifies the permissions to grant during creation. Default value: 'ALL'. ##### `istemplate` -Specifies that the database is a template, if set to `true`. +Specifies that the database is a template, if set to `true`. Default value: `false`. ##### `locale` -Overrides the locale during creation of the database. +Overrides the locale during creation of the database. Default value: The default defined during installation. ##### `owner` -Sets a user as the owner of the database. +Sets a user as the owner of the database. Default value: '$user' variable set in `postgresql::server` or `postgresql::globals`. @@ -1086,13 +1086,13 @@ Default value: '$user' variable set in `postgresql::server` or `postgresql::glob ##### `tablespace` -Defines the name of the tablespace to allocate the created database to. +Defines the name of the tablespace to allocate the created database to. Default value: PostgreSQL default. ##### `template` -Specifies the name of the template database from which to build this database. +Specifies the name of the template database from which to build this database. Defaults value: `template0`. @@ -1106,13 +1106,13 @@ Creates a database with no users and no permissions. ##### `dbname` -Sets the name of the database. +Sets the name of the database. Defaults value: The namevar. ##### `encoding` -Overrides the character set during creation of the database. +Overrides the character set during creation of the database. Default value: The default defined during installation. @@ -1136,13 +1136,13 @@ Default value: The '$user' variable set in `postgresql::server` or `postgresql:: ##### `tablespace` -Sets tablespace for where to create this database. +Sets tablespace for where to create this database. Default value: The default defined during installation. ##### `template` -Specifies the name of the template database from which to build this database. +Specifies the name of the template database from which to build this database. Default value: 'template0'. @@ -1152,7 +1152,7 @@ Manages grant-based access privileges for users, wrapping the `postgresql::serve #### `connect_settings` -Specifies a hash of environment variables used when connecting to a remote server. +Specifies a hash of environment variables used when connecting to a remote server. Default value: Connects to the local Postgres instance. @@ -1162,13 +1162,13 @@ Specifies the database to which you are granting access. ##### `privilege` -Specifies comma-separated list of privileges to grant. +Specifies comma-separated list of privileges to grant. Valid options: 'ALL', 'CREATE', 'CONNECT', 'TEMPORARY', 'TEMP'. ##### `psql_db` -Defines the database to execute the grant against. +Defines the database to execute the grant against. **This should not ordinarily be changed from the default** @@ -1176,7 +1176,7 @@ Default value: 'postgres'. ##### `psql_user` -Specifies the OS user for running `psql`. +Specifies the OS user for running `psql`. Default value: The default user for the module, usually 'postgres'. @@ -1192,6 +1192,10 @@ Manages a PostgreSQL extension. Specifies the database on which to activate the extension. +##### `schema` + +Specifies the schema on which to activate the extension. + ##### `ensure` Specifies whether to activate or deactivate the extension. @@ -1483,7 +1487,7 @@ The parameters are grouped into these three sections: ##### `target` Provides the target for the rule, and is generally an internal only property. - + **Use with caution.** #### postgresql::server::role @@ -1745,7 +1749,7 @@ This is the namevar. Required. -Specifies the action to create or destroy named slot. +Specifies the action to create or destroy named slot. Valid values: 'present', 'absent'. @@ -1799,7 +1803,7 @@ Default value: '', which generally uses the designated local Unix socket. Defines the port to use when connecting. -Default value: '' +Default value: '' ##### `run_as` @@ -1833,7 +1837,7 @@ This internal function converts a list of `pg_hba.conf` based ACLs (passed in as ### Tasks -The Postgresql module has an example task that allows a user to execute arbitary SQL against a database. Please refer to to the [PE documentation](https://puppet.com/docs/pe/2017.3/orchestrator/running_tasks.html) or [Bolt documentation](https://puppet.com/docs/bolt/latest/bolt.html) on how to execute a task. +The Postgresql module has an example task that allows a user to execute arbitary SQL against a database. Please refer to to the [PE documentation](https://puppet.com/docs/pe/2017.3/orchestrator/running_tasks.html) or [Bolt documentation](https://puppet.com/docs/bolt/latest/bolt.html) on how to execute a task. ## Limitations diff --git a/manifests/server/extension.pp b/manifests/server/extension.pp index 86f2f9df11..9fb100c246 100644 --- a/manifests/server/extension.pp +++ b/manifests/server/extension.pp @@ -2,6 +2,7 @@ define postgresql::server::extension ( $database, $extension = $name, + Optional[String[1]] $schema = undef, Optional[String[1]] $version = undef, String[1] $ensure = 'present', $package_name = undef, @@ -15,15 +16,15 @@ case $ensure { 'present': { $command = "CREATE EXTENSION \"${extension}\"" - $unless_comp = '=' + $unless_mod = '' $package_require = [] - $package_before = Postgresql_psql["Add ${extension} extension to ${database}"] + $package_before = Postgresql_psql["${database}: ${command}"] } 'absent': { $command = "DROP EXTENSION \"${extension}\"" - $unless_comp = '!=' - $package_require = Postgresql_psql["Add ${extension} extension to ${database}"] + $unless_mod = 'NOT ' + $package_require = Postgresql_psql["${database}: ${command}"] $package_before = [] } @@ -39,7 +40,7 @@ } } - postgresql_psql {"Add ${extension} extension to ${database}": + postgresql_psql { "${database}: ${command}": psql_user => $user, psql_group => $group, @@ -48,7 +49,34 @@ db => $database, command => $command, - unless => "SELECT t.count FROM (SELECT count(extname) FROM pg_extension WHERE extname = '${extension}') as t WHERE t.count ${unless_comp} 1", + unless => "SELECT 1 WHERE ${unless_mod}EXISTS (SELECT 1 FROM pg_extension WHERE extname = '${extension}')", + } + + if $ensure == 'present' and $schema { + $set_schema_command = "ALTER EXTENSION \"${extension}\" SET SCHEMA \"${schema}\"" + + postgresql_psql { "${database}: ${set_schema_command}": + command => $set_schema_command, + unless => @("END") + SELECT 1 + WHERE EXISTS ( + SELECT 1 + FROM pg_extension e + JOIN pg_namespace n ON e.extnamespace = n.oid + WHERE e.extname = '${extension}' AND + n.nspname = '${schema}' + ) + |-END + , + psql_user => $user, + psql_group => $group, + psql_path => $psql_path, + connect_settings => $connect_settings, + db => $database, + require => Postgresql_psql["${database}: ${command}"], + } + + Postgresql::Server::Schema <| db == $database and schema == $schema |> -> Postgresql_psql["${database}: ${set_schema_command}"] } if $package_name { diff --git a/spec/unit/defines/server/extension_spec.rb b/spec/unit/defines/server/extension_spec.rb index 6ce5de665a..efb4b8b59b 100644 --- a/spec/unit/defines/server/extension_spec.rb +++ b/spec/unit/defines/server/extension_spec.rb @@ -27,14 +27,23 @@ context "with mandatory arguments only" do it { - is_expected.to contain_postgresql_psql('Add postgis extension to template_postgis').with({ + is_expected.to contain_postgresql_psql('template_postgis: CREATE EXTENSION "postgis"').with({ :db => 'template_postgis', :command => 'CREATE EXTENSION "postgis"', - :unless => "SELECT t.count FROM (SELECT count(extname) FROM pg_extension WHERE extname = 'postgis') as t WHERE t.count = 1", }).that_requires('Postgresql::Server::Database[template_postgis]') } end + context "when schema is specified" do + let (:params) { super().merge({ + :schema => 'pg_catalog', + }) } + + it { + is_expected.to contain_postgresql_psql('template_postgis: ALTER EXTENSION "postgis" SET SCHEMA "pg_catalog"') + } + end + context "when setting package name" do let (:params) { super().merge({ :package_name => 'postgis', @@ -44,7 +53,7 @@ is_expected.to contain_package('postgis').with({ :ensure => 'present', :name => 'postgis', - }).that_comes_before('Postgresql_psql[Add postgis extension to template_postgis]') + }).that_comes_before('Postgresql_psql[template_postgis: CREATE EXTENSION "postgis"]') } end @@ -55,10 +64,9 @@ }) } it { - is_expected.to contain_postgresql_psql('Add postgis extension to template_postgis').with({ + is_expected.to contain_postgresql_psql('template_postgis: DROP EXTENSION "postgis"').with({ :db => 'template_postgis', :command => 'DROP EXTENSION "postgis"', - :unless => "SELECT t.count FROM (SELECT count(extname) FROM pg_extension WHERE extname = 'postgis') as t WHERE t.count != 1", }).that_requires('Postgresql::Server::Database[template_postgis]') } @@ -75,10 +83,9 @@ }) } it { - is_expected.to contain_postgresql_psql('Add postgis extension to template_postgis').with({ + is_expected.to contain_postgresql_psql('template_postgis: DROP EXTENSION "postgis"').with({ :db => 'template_postgis', :command => 'DROP EXTENSION "postgis"', - :unless => "SELECT t.count FROM (SELECT count(extname) FROM pg_extension WHERE extname = 'postgis') as t WHERE t.count != 1", }).that_requires('Postgresql::Server::Database[template_postgis]') } @@ -86,7 +93,7 @@ is_expected.to contain_package('postgis').with({ :ensure => 'present', :name => 'postgis', - }).that_requires('Postgresql_psql[Add postgis extension to template_postgis]') + }).that_requires('Postgresql_psql[template_postgis: DROP EXTENSION "postgis"]') } end end @@ -150,10 +157,9 @@ context "with mandatory arguments only" do it { - is_expected.to contain_postgresql_psql('Add postgis extension to template_postgis2').with({ + is_expected.to contain_postgresql_psql('template_postgis2: CREATE EXTENSION "postgis"').with({ :db => 'template_postgis2', :command => 'CREATE EXTENSION "postgis"', - :unless => "SELECT t.count FROM (SELECT count(extname) FROM pg_extension WHERE extname = 'postgis') as t WHERE t.count = 1", }).that_requires('Postgresql::Server::Database[template_postgis2]') } end