diff --git a/.vscode/settings.json b/.vscode/settings.json
index 87311dd..a8cccd4 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -9,6 +9,7 @@
"csvlog",
"csvlogs",
"datestyle",
+ "datname",
"ecdh",
"fdatasync",
"geqo",
@@ -34,6 +35,7 @@
"multixact",
"naptime",
"nestloop",
+ "onlyif",
"partitionwise",
"pghba",
"pgsql",
@@ -47,6 +49,7 @@
"tablespaces",
"tidscan",
"timezonesets",
+ "trgm",
"usename",
"walsender",
"writethrough",
diff --git a/doc/_index.html b/doc/_index.html
index b5002f7..1f9f481 100644
--- a/doc/_index.html
+++ b/doc/_index.html
@@ -139,6 +139,11 @@
P
+ -
+ postgresql_cd::server::databases::db_df
+
+
+
-
postgresql_cd::server::pghba::pg_hba_rule
diff --git a/doc/puppet_classes/postgresql_cd_3A_3Aparams.html b/doc/puppet_classes/postgresql_cd_3A_3Aparams.html
index 6913895..bf84673 100644
--- a/doc/puppet_classes/postgresql_cd_3A_3Aparams.html
+++ b/doc/puppet_classes/postgresql_cd_3A_3Aparams.html
@@ -396,6 +396,24 @@
—
Whether to manage roles
+
+
+
+
+ -
+
+ pl_manage_databases
+
+
+ (Boolean)
+
+
+ (defaults to: true)
+
+
+ —
+
+
Whether to manage databases
@@ -411,7 +429,6 @@
-24
25
26
27
@@ -455,10 +472,12 @@
65
66
67
-68
+68
+69
+70
- # File 'manifests/params.pp', line 24
+ # File 'manifests/params.pp', line 25
class postgresql_cd::params (
@@ -484,6 +503,7 @@ class postgresql_cd::params (
String $pl_server_key = 'server.key',
String $pl_ca_crt = 'root.crt',
Boolean $pl_manage_roles = true,
+ Boolean $pl_manage_databases = true,
) {
$fqdn = $facts['networking']['fqdn']
diff --git a/doc/puppet_defined_type_list.html b/doc/puppet_defined_type_list.html
index b8f2071..9ade849 100644
--- a/doc/puppet_defined_type_list.html
+++ b/doc/puppet_defined_type_list.html
@@ -40,14 +40,21 @@
- -
+
-
+
+
+
+
+ -
- -
+
-
diff --git a/doc/puppet_defined_types/postgresql_cd_3A_3Aserver_3A_3Adatabases_3A_3Adb_df.html b/doc/puppet_defined_types/postgresql_cd_3A_3Aserver_3A_3Adatabases_3A_3Adb_df.html
new file mode 100644
index 0000000..47fc1b3
--- /dev/null
+++ b/doc/puppet_defined_types/postgresql_cd_3A_3Aserver_3A_3Adatabases_3A_3Adb_df.html
@@ -0,0 +1,261 @@
+
+
+
+
+
+
+ Defined Type: postgresql_cd::server::databases::db_df
+
+ — Documentation by YARD 0.9.36
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Defined Type: postgresql_cd::server::databases::db_df
+
+
+ - Defined in:
+ -
+ manifests/server/databases/db_df.pp
+
+
+
+
+ Summary
+ define manages databases
+
+ Overview
+
+
+
+ postgresql_cd::server::databases::db_df Module name: postgresql_cd Author: Arne Teuke (arne_teuke@confdroid.com.com)
+
+
+
+
+
+
+
+
+
+
+
+
+
+13
+14
+15
+16
+17
+18
+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
+ |
+
+ # File 'manifests/server/databases/db_df.pp', line 13
+
+define postgresql_cd::server::databases::db_df (
+
+ Optional[String] $pl_db_name = undef,
+ Optional[String] $pl_owner_name = undef,
+ Optional[String] $pl_db_action = undef,
+ String $pl_db_extension = 'pg_trgm',
+) {
+ $pl_manage_databases = $postgresql_cd::params::pl_manage_databases
+
+ if $pl_manage_databases == true {
+ # create databases
+
+ if $pl_db_action == 'CREATE DATABASE' {
+ exec { "create_database_${name}":
+ command => template('postgresql_cd/server/databases/db_create_sql.erb'),
+ user => 'postgres',
+ path => ['/usr/bin','/bin'],
+ cwd => '/tmp',
+ unless => template('postgresql_cd/server/databases/unless_db_sql.erb'),
+ }
+ }
+
+ # Drop databases
+
+ if $pl_db_action == 'DROP DATABASE' {
+ exec { "drop_database_${name}":
+ command => template('postgresql_cd/server/databases/db_drop_sql.erb'),
+ user => 'postgres',
+ path => ['/usr/bin','/bin'],
+ cwd => '/tmp',
+ onlyif => template('postgresql_cd/server/databases/unless_drop_sql.erb'),
+ }
+ }
+ }
+}
+ |
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/manifests/params.pp b/manifests/params.pp
index 724ede5..a6c7645 100644
--- a/manifests/params.pp
+++ b/manifests/params.pp
@@ -19,6 +19,7 @@
# @param [String] pl_server_key the name of the server key
# @param [String] pl_ca_crt the name of the CA crt
# @param [Boolean] pl_manage_roles Whether to manage roles
+# @param [Boolean] pl_manage_databases Whether to manage databases
# @summary Class contains all parameters for the postgresql_cd module.
##############################################################################
class postgresql_cd::params (
@@ -45,6 +46,7 @@ class postgresql_cd::params (
String $pl_server_key = 'server.key',
String $pl_ca_crt = 'root.crt',
Boolean $pl_manage_roles = true,
+ Boolean $pl_manage_databases = true,
) {
$fqdn = $facts['networking']['fqdn']
diff --git a/manifests/server/databases/db_df.pp b/manifests/server/databases/db_df.pp
new file mode 100644
index 0000000..bf8fda0
--- /dev/null
+++ b/manifests/server/databases/db_df.pp
@@ -0,0 +1,47 @@
+## postgresql_cd::server::databases::db_df
+# Module name: postgresql_cd
+# Author: Arne Teuke (arne_teuke@confdroid.com.com)
+# @summary define manages databases
+# @see https://www.postgresql.org/docs/9.6/static/managing-databases.html
+# @param [String] pl_db_name the name of the database to be created.
+# @param [String] pl_owner_name the name of the owner for the database
+# (optional), if none specified, the postgresql defaults will apply.
+# @param [String] pl_db_action whether to create or drop the database.
+# 'CREATE DATABASE' creates it, 'DROP DATABASE' drops it.
+# @param [String] pl_db_extension
+##############################################################################
+define postgresql_cd::server::databases::db_df (
+
+ Optional[String] $pl_db_name = undef,
+ Optional[String] $pl_owner_name = undef,
+ Optional[String] $pl_db_action = undef,
+ String $pl_db_extension = 'pg_trgm',
+) {
+ $pl_manage_databases = $postgresql_cd::params::pl_manage_databases
+
+ if $pl_manage_databases == true {
+ # create databases
+
+ if $pl_db_action == 'CREATE DATABASE' {
+ exec { "create_database_${name}":
+ command => template('postgresql_cd/server/databases/db_create_sql.erb'),
+ user => 'postgres',
+ path => ['/usr/bin','/bin'],
+ cwd => '/tmp',
+ unless => template('postgresql_cd/server/databases/unless_db_sql.erb'),
+ }
+ }
+
+ # Drop databases
+
+ if $pl_db_action == 'DROP DATABASE' {
+ exec { "drop_database_${name}":
+ command => template('postgresql_cd/server/databases/db_drop_sql.erb'),
+ user => 'postgres',
+ path => ['/usr/bin','/bin'],
+ cwd => '/tmp',
+ onlyif => template('postgresql_cd/server/databases/unless_drop_sql.erb'),
+ }
+ }
+ }
+}
diff --git a/templates/server/databases/db_create_sql.erb b/templates/server/databases/db_create_sql.erb
new file mode 100644
index 0000000..c52e69b
--- /dev/null
+++ b/templates/server/databases/db_create_sql.erb
@@ -0,0 +1,2 @@
+psql -U postgres -tc "SELECT 1 FROM pg_database WHERE datname = '<%= @pl_db_name %>'" | grep -q 1 || psql -U postgres -c "CREATE DATABASE <%= @pl_db_name %> OWNER '<%= @pl_owner_name %>' "
+psql -U postgres <%= @pl_db_name %> -c 'create extension if not exists <%= @pl_db_extension %>'
diff --git a/templates/server/databases/db_drop_sql.erb b/templates/server/databases/db_drop_sql.erb
new file mode 100644
index 0000000..62e5f71
--- /dev/null
+++ b/templates/server/databases/db_drop_sql.erb
@@ -0,0 +1 @@
+dropdb -U postgres <%= @pl_db_name %> --if-exists
diff --git a/templates/server/databases/unless_db_sql.erb b/templates/server/databases/unless_db_sql.erb
new file mode 100644
index 0000000..0b7e57d
--- /dev/null
+++ b/templates/server/databases/unless_db_sql.erb
@@ -0,0 +1 @@
+psql -U postgres -c "SELECT datname FROM pg_database WHERE datname='<%= @pl_db_name %>' " | grep -q 1
diff --git a/templates/server/databases/unless_drop_sql.erb b/templates/server/databases/unless_drop_sql.erb
new file mode 100644
index 0000000..0b7e57d
--- /dev/null
+++ b/templates/server/databases/unless_drop_sql.erb
@@ -0,0 +1 @@
+psql -U postgres -c "SELECT datname FROM pg_database WHERE datname='<%= @pl_db_name %>' " | grep -q 1
|