def _run_initdb(name,
        auth='password',
        user=None,
        password=None,
        encoding='UTF8',
        locale=None,
        runas=None):
    if runas is None:
        if 'FreeBSD' in __grains__['os_family']:
            runas = 'pgsql'
        if 'OpenBSD' in __grains__['os_family']:
            runas = '_postgresql'
        else:
            runas = 'postgres'

    if user is None:
        user = runas

    cmd = [
        salt.utils.which('initdb'),
        '--pgdata={0}'.format(name),
        '--username={0}'.format(user),
        '--auth={0}'.format(auth),
        '--encoding={0}'.format(encoding),
        ]

    if locale is not None:
        cmd.append('--locale={0}'.format(locale))

    if password is not None:
        pgpassfile = salt.utils.mkstemp(text=True)
        with salt.utils.fopen(pgpassfile, 'w') as fp_:
            fp_.write('{0}'.format(password))
            __salt__['file.chown'](pgpassfile, runas, '')
        cmd.extend([
            '--pwfile={0}'.format(pgpassfile),
        ])

    kwargs = dict(runas=runas, clean_env=True)
    cmdstr = ' '.join([pipes.quote(c) for c in cmd])
    ret = __salt__['cmd.run_all'](cmdstr, python_shell=False, **kwargs)

    if ret.get('retcode', 0) != 0:
        log.error('Error initilizing the postgres data directory')

    if password is not None and not __salt__['file.remove'](pgpassfile):
        log.warning('Removal of PGPASSFILE failed')

    return ret
Helper function to call initdb
dataset/ETHPy150Open saltstack/salt/salt/modules/postgres.py/_run_initdb