18

I want to create a database user for my setup fabric script but createuser has interactive password entering and seams not to like fabric.

user320080
  • 183
  • 1
  • 4

3 Answers3

35

To extend the answer with a Fabric example...

# In fabfile.py
def create_database():
    """Creates role and database"""
    db_user = get_user()  # define these
    db_pass = get_pass()
    db_name = get_db_name()
    sudo('psql -c "CREATE USER %s WITH NOCREATEDB NOCREATEUSER " \
         "ENCRYPTED PASSWORD E\'%s\'"' % (db_user, db_pass), user='postgres')
    sudo('psql -c "CREATE DATABASE %s WITH OWNER %s"' % (
         db_name, db_user), user='postgres')
Community
  • 1
  • 1
robhudson
  • 2,658
  • 1
  • 21
  • 19
  • 2
    Upwote. But did you really mean table in `db_table = get_table()`? Looks like you meant a database name. – Vlad T. Feb 22 '18 at 11:34
8

Just use plain SQL to create a new user:

CREATE ROLE user_name WITH ENCRYPTED PASSWORD 'your password';
Frank Heikens
  • 117,544
  • 24
  • 142
  • 135
1

This is probably of some use without having to write your own modules or you could use it as reference.

from fabtools import require

require.postgres.create_db ???

https://fabtools.readthedocs.org/en/0.19.0/api/require/postgres.html?highlight=postgres#module-fabtools.require.postgres

user805981
  • 9,979
  • 8
  • 44
  • 64