user320080
user320080

Reputation: 183

How do I create a postgresql user with fabric

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

Upvotes: 18

Views: 4050

Answers (3)

robhudson
robhudson

Reputation: 2658

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')

Upvotes: 35

user805981
user805981

Reputation: 11059

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

Upvotes: 1

Frank Heikens
Frank Heikens

Reputation: 127396

Just use plain SQL to create a new user:

CREATE ROLE user_name WITH ENCRYPTED PASSWORD 'your password';

Upvotes: 8

Related Questions