fabric postgresql create database
What would be equivalent of this in postgresql
run('echo "CREATE DATABASE %s;"|mysql --batch --user=%s --password=%s --host=%s' % (dataname, env.mysqluser, env.mysqlpassword, env.mysqlhost), pty=True)
The above works fine for creating mysql database, but 开发者_如何转开发how would you create one in postgresql without getting stuck at password prompt.
Here's how I do it:-
run('psql -U postgres -c "CREATE ROLE {0} WITH PASSWORD \'{1}\' NOSUPERUSER CREATEDB NOCREATEROLE LOGIN;"'.format(env.psql_user, env.psql_password))
run('psql -U postgres -c "CREATE DATABASE {0} WITH OWNER={1} TEMPLATE=template0 ENCODING=\'utf-8\';"'.format(env.psql_db, env.psql_user))
You can:
- set PGPASSWORD
- use .pgpass
- change pg_hba.conf
Choose whatever suits you best. I would probably go with .pgpass.
psql reads ~/.pgpass
You can also use non-password-based authentication methods. Consult PostgreSQL's documentation for more.
精彩评论