Defined Type: postgresql::server::extension

Defined in:
manifests/server/extension.pp

Summary

Activate an extension on a postgresql database.

Overview

Parameters:

  • database (Any)

    Specifies the database on which to activate the extension.

  • extension (Any) (defaults to: $name)

    Specifies the extension to activate. If left blank, uses the name of the resource.

  • schema (Optional[String[1]]) (defaults to: undef)

    Specifies the schema on which to activate the extension.

  • version (Optional[String[1]]) (defaults to: undef)

    Specifies the version of the extension which the database uses. When an extension package is updated, this does not automatically change the effective version in each database. This needs be updated using the PostgreSQL-specific SQL ALTER EXTENSION… version may be set to latest, in which case the SQL ALTER EXTENSION “extension” UPDATE is applied to this database (only). version may be set to a specific version, in which case the extension is updated using ALTER EXTENSION “extension” UPDATE TO ‘version’ eg. If extension is set to postgis and version is set to 2.3.3, this will apply the SQL ALTER EXTENSION “postgis” UPDATE TO ‘2.3.3’ to this database only. version may be omitted, in which case no ALTER EXTENSION… SQL is applied, and the version will be left unchanged.

  • ensure (String[1]) (defaults to: 'present')

    Specifies whether to activate or deactivate the extension. Valid options: ‘present’ or ‘absent’.

  • package_name (Any) (defaults to: undef)

    Specifies a package to install prior to activating the extension.

  • package_ensure (Any) (defaults to: undef)

    Overrides default package deletion behavior. By default, the package specified with package_name is installed when the extension is activated and removed when the extension is deactivated. To override this behavior, set the ensure value for the package.

  • port (Optional[Integer]) (defaults to: undef)

    Port to use when connecting.

  • connect_settings (Any) (defaults to: postgresql::default('default_connect_settings'))

    Specifies a hash of environment variables used when connecting to a remote server.

  • database_resource_name (Any) (defaults to: $database)

    Specifies the resource name of the DB being managed. Defaults to the parameter $database, if left blank.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'manifests/server/extension.pp', line 19

define postgresql::server::extension (
  $database,
  $extension                   = $name,
  Optional[String[1]] $schema  = undef,
  Optional[String[1]] $version = undef,
  String[1] $ensure            = 'present',
  $package_name                = undef,
  $package_ensure              = undef,
  Optional[Integer] $port      = undef,
  $connect_settings            = postgresql::default('default_connect_settings'),
  $database_resource_name      = $database,
) {
  $user             = postgresql::default('user')
  $group            = postgresql::default('group')
  $psql_path        = postgresql::default('psql_path')

  if( $database != 'postgres' ) {
    # The database postgres cannot managed by this module, so it is exempt from this dependency
    $default_psql_require = Postgresql::Server::Database[$database_resource_name]

    Postgresql_psql {
      require => $default_psql_require,
    }
  } else {
    $default_psql_require = undef
  }

  case $ensure {
    'present': {
      $command = "CREATE EXTENSION \"${extension}\""
      $unless_mod = undef
      $psql_cmd_require = $package_name ? {
        undef   => $default_psql_require,
        default => [$default_psql_require, Package[$package_name]],
      }
      $psql_cmd_before = []
    }

    'absent': {
      $command = "DROP EXTENSION \"${extension}\""
      $unless_mod = 'NOT '
      $psql_cmd_require = $default_psql_require
      $psql_cmd_before = $package_name ? {
        undef   => [],
        default => Package[$package_name],
      }
    }

    default: {
      fail("Unknown value for ensure '${ensure}'.")
    }
  }

  #
  # Port, order of precedence: $port parameter, $connect_settings[PGPORT], $postgresql::server::port
  #
  if $port != undef {
    $port_override = $port
  } elsif $connect_settings != undef and has_key( $connect_settings, 'PGPORT') {
    $port_override = undef
  } else {
    $port_override = $postgresql::server::port
  }

  postgresql_psql { "${database}: ${command}":

    psql_user        => $user,
    psql_group       => $group,
    psql_path        => $psql_path,
    connect_settings => $connect_settings,

    db               => $database,
    port             => $port_override,
    command          => $command,
    unless           => "SELECT 1 WHERE ${unless_mod}EXISTS (SELECT 1 FROM pg_extension WHERE extname = '${extension}')",
    require          => $psql_cmd_require,
    before           => $psql_cmd_before,
  }

  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,
      port             => $port_override,
      require          => Postgresql_psql["${database}: ${command}"],
    }

    Postgresql::Server::Schema <| db == $database and schema == $schema |> -> Postgresql_psql["${database}: ${set_schema_command}"]
  }

  if $package_name {
    $_package_ensure = $package_ensure ? {
      undef   => $ensure,
      default => $package_ensure,
    }

    ensure_packages($package_name, {
        ensure  => $_package_ensure,
        tag     => 'puppetlabs-postgresql',
    })
  }
  if $version {
    if $version == 'latest' {
      $alter_extension_sql = "ALTER EXTENSION \"${extension}\" UPDATE"
      $update_unless = "SELECT 1 FROM pg_available_extensions WHERE name = '${extension}' AND default_version = installed_version"
    } else {
      $alter_extension_sql = "ALTER EXTENSION \"${extension}\" UPDATE TO '${version}'"
      $update_unless = "SELECT 1 FROM pg_extension WHERE extname='${extension}' AND extversion='${version}'"
    }
    postgresql_psql { "${database}: ${alter_extension_sql}":
      db               => $database,
      port             => $port_override,
      psql_user        => $user,
      psql_group       => $group,
      psql_path        => $psql_path,
      connect_settings => $connect_settings,
      command          => $alter_extension_sql,
      unless           => $update_unless,
    }
  }
}