Accept port arg when running postgresql script

This commit is contained in:
Lance Edgar 2018-03-01 18:51:45 -06:00
parent 764af27c31
commit 90e5133995

View file

@ -62,18 +62,20 @@ def sql(sql, database='', port=None):
return sudo(cmd, shell=False)
def script(path, database='', user=None, password=None):
def script(path, database='', port=None, user=None, password=None):
"""
Execute a SQL script. By default this will run as 'postgres' user, but can
use PGPASSWORD authentication if necessary.
"""
port = '--port={}'.format(port) if port else ''
if user and password:
with hide('running'):
return sudo(" PGPASSWORD='{}' psql --host=localhost --username='{}' --file='{}' {}".format(
password, user, path, database))
kw = dict(pw=password, user=user, port=port, path=path, db=database)
return sudo(" PGPASSWORD='{pw}' psql --host=localhost {port} --username='{user}' --file='{path}' {db}".format(**kw))
else: # run as postgres
return sudo("sudo -u postgres psql --file='{}' {}".format(path, database), shell=False)
kw = dict(port=port, path=path, db=database)
return sudo("sudo -u postgres psql {port} --file='{path}' {db}".format(**kw), shell=False)
def user_exists(name, port=None):