Requires: PostgreSQLClient

Example Usage

This is what a charm using this relation would look like:

from charmhelpers.core import hookenv
from charmhelpers.core.reactive import hook
from charmhelpers.core.reactive import when
from charmhelpers.core.reactive import when_file_changed
from charmhelpers.core.reactive import set_state
from charmhelpers.core.reactive import remove_state

@hook('db-relation-joined')
def request_db(pgsql):
    pgsql.change_database_name('mydb')
    pgsql.request_roles('myrole', 'otherrole')

@hook('config-changed')
def check_admin_pass():
    admin_pass = hookenv.config('admin-pass')
    if admin_pass:
        set_state('admin-pass')
    else:
        remove_state('admin-pass')

@when('db.database.available', 'admin-pass')
def render_config(pgsql):
    render_template('app-config.j2', '/etc/app.conf', {
        'db_conn': pgsql.connection_string(),
        'admin_pass': hookenv.config('admin-pass'),
    })

@when_file_changed('/etc/app.conf')
def restart_service():
    hookenv.service_restart('myapp')

Reference

class requires.PostgreSQLClient(relation_name, conversations=None)[source]
change_database_name(dbname)[source]

Tell the PostgreSQL server to provide us with a database with a specific name.

Parameters:dbname (str) – New name for the database to use.
connection_string()[source]

Get the connection string, if available, or None.

database()

Get the database, if available, or None.

host()

Get the host, if available, or None.

password()

Get the password, if available, or None.

port()

Get the port, if available, or None.

request_roles(*roles)[source]

Tell the PostgreSQL server to provide our user with a certain set of roles.

Parameters:roles (list) – One or more role names to give to this service’s user.
schema_password()

Get the schema_password, if available, or None.

schema_user()

Get the schema_user, if available, or None.

user()

Get the user, if available, or None.