Defined Type: postgresql::db

Defined in:
modules/postgresql/manifests/db.pp

Overview

Definition: postgresql::db

Manages a PostgreSQL database.

Parameters:

ensure

'present' to create the database, 'absent' to delete it

owner

User who will own the database (defaults to 'postgres')

Actions:

Create/drop database

Requires:

Class['postgresql::server']

Sample Usage:

postgresql::db { 'mydb': }

Parameters:

  • ensure (Wmflib::Ensure) (defaults to: present)
  • owner (String) (defaults to: 'postgres')


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
# File 'modules/postgresql/manifests/db.pp', line 23

define postgresql::db(
    Wmflib::Ensure $ensure = present,
    String         $owner  = 'postgres',
) {

    require postgresql::server

    $name_safe = regsubst($title, '[\W_]', '_', 'G')

    $db_sql = "SELECT datname from pg_catalog.pg_database where datname = '${name_safe}'"
    $db_exists = "/usr/bin/test -n \"\$( /usr/bin/psql -At -c \"${db_sql}\")\""

    if $ensure == 'present' {
        exec { "create_postgres_db_${name_safe}":
            command => "/usr/bin/createdb --owner='${owner}' '${title}'",
            unless  => $db_exists,
            user    => 'postgres',
        }
    } else {
        exec { "drop_postgres_db_${name_safe}":
            command => "/usr/bin/dropdb '${title}'",
            onlyif  => $db_exists,
            user    => 'postgres',
        }
    }
}