postgresql-database-exists

Example:

# Create the db 'my_database' and user 'freckles'.
- postgresql-database-exists:
    db_name: my_database
    db_user: freckles
    db_user_password: md5aee63ef475154b1b0461fc508db22950

Description

Ensures a database with the provided name is present on a PostgreSQL server.

The PostgreSQL service itself won't be installed, use the 'postgresql-service' frecklet before this if necessary.

If db_user is provided, its value will be used as the owner of the database.

Resources

Variables

Name Type Default Description

db_name

string --

The name of the database to use from the dump. Required

db_encoding

string UTF-8

The encoding of the db (default: 'UTF-8').

db_lc_collate

string --

Collation order (LC_COLLATE) to use in the database. Must match collation order of template database unless template0 is used as template.

db_lc_ctype

string --

Character classification (LC_CTYPE) to use in the database (e.g. lower, upper, ...) Must match LC_CTYPE of template database unless template0 is used as template.

db_template

string --

The template used to create the database.

db_user

string --

The name of the database user ('role' in postgres).

db_user_password

string --

The password needs to be passed in hashed form, please check the [Postgresql documentation](

Examples

Example 1

Create the db 'my_database' and user 'freckles'.

Code
- postgresql-database-exists:
    db_name: my_database
    db_user: freckles
    db_user_password: md5aee63ef475154b1b0461fc508db22950

Code

doc:
  short_help: Makes sure a specified database exists on a PostgreSQL server.
  help: |
    Ensures a database with the provided name is present on a PostgreSQL server.

    The PostgreSQL service itself won't be installed, use the 'postgresql-service' frecklet before this if necessary.

    If ``db_user`` is provided, its value will be used as the owner of the database.

  references:
    Postgresql createuser documentation: https://www.postgresql.org/docs/current/app-createuser.html
    Postgresql 'create role' documentation: https://www.postgresql.org/docs/current/sql-createrole.html
    Ansible postgresql_user documentation: https://docs.ansible.com/ansible/latest/modules/postgresql_user_module.html
    Ansible postgresql_db documentation: https://docs.ansible.com/ansible/latest/modules/postgresql_db_module.html
  examples:
  - title: Create the db 'my_database' and user 'freckles'.
    vars:
      db_name: my_database
      db_user: freckles
      db_user_password: md5aee63ef475154b1b0461fc508db22950

args:
  db_user:
    doc:
      short_help: The name of the database user ('role' in postgres).
    type: string
    required: false
  db_user_password:
    doc:
      short_help: The (hashed) password for the database user ('role' in PostgreSQL).
      help: |
        The password needs to be passed in hashed form, please check the [Postgresql documentation](
    type: string
    required: false
    secret: true
  db_name:
    doc:
      short_help: The name of the database to use from the dump.
    type: string
    required: true
  db_template:
    doc:
      short_help: The template used to create the database.
    type: string
    required: false
  db_encoding:
    doc:
      short_help: "The encoding of the db (default: 'UTF-8')."
    type: string
    default: UTF-8
    required: false
    cli:
      metavar: ENCODING
  db_lc_collate:
    doc:
      short_help: The collation order to use in the database.
      help: |
        Collation order (LC_COLLATE) to use in the database. Must match collation order of template database unless template0 is used as template.
    type: string
    required: false
    cli:
      metavar: LC_COLLATE
  db_lc_ctype:
    doc:
      short_help: Character classification (LC_CTYPE) to use in the database.
      help: |
        Character classification (LC_CTYPE) to use in the database (e.g. lower, upper, ...) Must match LC_CTYPE of template database unless template0 is used as template.
    type: string
    required: false
    cli:
      metavar: LC_CTYPE

meta:
  tags:
  - postgresql
  - database

frecklets:
- postgresql-user-exists:
    frecklet::skip: '{{:: db_user | true_if_empty ::}}'
    db_user: '{{:: db_user ::}}'
    db_user_password: '{{:: db_user_password ::}}'
- task:
    become: true
    become_user: postgres
  frecklet:
    name: postgresql_db
    type: ansible-module
#      skip: "{{:: db_import ::}}"
    properties:
      idempotent: true
      elevated: true
      internet: false
    desc:
      short: 'create database: {{:: db_name ::}}'
      references:
        "'postgresql_db' Ansible module": https://docs.ansible.com/ansible/latest/modules/postgresql_db_module.html
  vars:
    name: '{{:: db_name ::}}'
    owner: '{{:: db_user ::}}'
    encoding: '{{:: db_encoding ::}}'
    template: '{{:: db_template ::}}'
    lc_collate: '{{:: db_lc_collate ::}}'
    lc_ctype: '{{:: db_lc_ctype ::}}'
frecklecute postgresql-database-exists --help

Usage: frecklecute postgresql-database-exists [OPTIONS]

  Ensures a database with the provided name is present on a PostgreSQL
  server.

  The PostgreSQL service itself won't be installed, use the 'postgresql-
  service' frecklet before this if necessary.

  If ``db_user`` is provided, its value will be used as the owner of the
  database.

Options:
  --db-name DB_NAME               The name of the database to use from the
                                  dump.  [required]
  --db-encoding ENCODING          The encoding of the db (default: 'UTF-8').
  --db-lc-collate LC_COLLATE      The collation order to use in the database.
  --db-lc-ctype LC_CTYPE          Character classification (LC_CTYPE) to use
                                  in the database.
  --db-template DB_TEMPLATE       The template used to create the database.
  --db-user DB_USER               The name of the database user ('role' in
                                  postgres).
  --db-user-password DB_USER_PASSWORD
                                  The (hashed) password for the database user
                                  ('role' in PostgreSQL).
  --help                          Show this message and exit.
# -*- coding: utf-8 -*-


#
# module path: pycklets.postgresql_database_exists.PostgresqlDatabaseExists
#


from dataclasses import dataclass
from pyckles import AutoPycklet
from typing import *    # noqa

@dataclass
class PostgresqlDatabaseExists(AutoPycklet):
    """Ensures a database with the provided name is present on a PostgreSQL server.

     The PostgreSQL service itself won't be installed, use the 'postgresql-service' frecklet before this if necessary.

     If ``db_user`` is provided, its value will be used as the owner of the database.

       Args:
         db_encoding: The encoding of the db (default: 'UTF-8').
         db_lc_collate: The collation order to use in the database.
         db_lc_ctype: Character classification (LC_CTYPE) to use in the database.
         db_name: The name of the database to use from the dump.
         db_template: The template used to create the database.
         db_user: The name of the database user ('role' in postgres).
         db_user_password: The (hashed) password for the database user ('role' in PostgreSQL).

    """

    FRECKLET_ID = "postgresql-database-exists"

    db_encoding: str = None
    db_lc_collate: str = None
    db_lc_ctype: str = None
    db_name: str = None
    db_template: str = None
    db_user: str = None
    db_user_password: str = None


    def __post_init__(self):
        super(PostgresqlDatabaseExists, self).__init__(var_names=["db_encoding", "db_lc_collate", "db_lc_ctype", "db_name", "db_template", "db_user", "db_user_password"])


frecklet_class = PostgresqlDatabaseExists
# -*- coding: utf-8 -*-


#
# module path: pycklets.postgresql_database_exists.PostgresqlDatabaseExists
#


from pyckles import AutoPycklet

class PostgresqlDatabaseExists(AutoPycklet):
    """Ensures a database with the provided name is present on a PostgreSQL server.

     The PostgreSQL service itself won't be installed, use the 'postgresql-service' frecklet before this if necessary.

     If ``db_user`` is provided, its value will be used as the owner of the database.

       Args:
         db_encoding: The encoding of the db (default: 'UTF-8').
         db_lc_collate: The collation order to use in the database.
         db_lc_ctype: Character classification (LC_CTYPE) to use in the database.
         db_name: The name of the database to use from the dump.
         db_template: The template used to create the database.
         db_user: The name of the database user ('role' in postgres).
         db_user_password: The (hashed) password for the database user ('role' in PostgreSQL).

    """

    FRECKLET_ID = "postgresql-database-exists"

    def __init__(self, db_encoding="UTF-8", db_lc_collate=None, db_lc_ctype=None, db_name=None, db_template=None, db_user=None, db_user_password=None):

        super(PostgresqlDatabaseExists, self).__init__(var_names=["db_encoding", "db_lc_collate", "db_lc_ctype", "db_name", "db_template", "db_user", "db_user_password"])
        self._db_encoding = db_encoding
        self._db_lc_collate = db_lc_collate
        self._db_lc_ctype = db_lc_ctype
        self._db_name = db_name
        self._db_template = db_template
        self._db_user = db_user
        self._db_user_password = db_user_password

    @property
    def db_encoding(self):
        return self._db_encoding

    @db_encoding.setter
    def db_encoding(self, db_encoding):
        self._db_encoding = db_encoding

    @property
    def db_lc_collate(self):
        return self._db_lc_collate

    @db_lc_collate.setter
    def db_lc_collate(self, db_lc_collate):
        self._db_lc_collate = db_lc_collate

    @property
    def db_lc_ctype(self):
        return self._db_lc_ctype

    @db_lc_ctype.setter
    def db_lc_ctype(self, db_lc_ctype):
        self._db_lc_ctype = db_lc_ctype

    @property
    def db_name(self):
        return self._db_name

    @db_name.setter
    def db_name(self, db_name):
        self._db_name = db_name

    @property
    def db_template(self):
        return self._db_template

    @db_template.setter
    def db_template(self, db_template):
        self._db_template = db_template

    @property
    def db_user(self):
        return self._db_user

    @db_user.setter
    def db_user(self, db_user):
        self._db_user = db_user

    @property
    def db_user_password(self):
        return self._db_user_password

    @db_user_password.setter
    def db_user_password(self, db_user_password):
        self._db_user_password = db_user_password



frecklet_class = PostgresqlDatabaseExists