I want to create a database user for my setup fabric script but createuser has interactive password entering and seams not to like fabric.
Asked
Active
Viewed 3,965 times
3 Answers
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')
-
2Upwote. 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
-
-
Connect with the psql client and send the query. http://www.postgresql.org/docs/8.4/interactive/app-psql.html – Frank Heikens Apr 19 '10 at 12:41
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 ???

user805981
- 9,979
- 8
- 44
- 64