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 (Any) (defaults to: present)
  • owner (Any) (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
49
# File 'modules/postgresql/manifests/db.pp', line 23

define postgresql::db(
    $ensure    = present,
    $owner     = 'postgres',
) {
    validate_ensure($ensure)

    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',
        }
    }
}