Switching users in Fabric always prompt for a password

77 Views Asked by At

I've tried many ways to switch users to do a restore backup for postgres using fabric.

This is my current code:

# -*- coding: utf-8 -*-
from datetime import datetime
from fabric.api import run, env, settings, hosts, execute
from fabric.operations import get

...

@hosts(['127.0.0.1'])
def restore_localdb(dump_file):
    with settings(user="postgres"):
        run('dropdb potato')
        run('createdb -E UTF8 -O potato potato')

    run('psql -U potato -h localhost potato -W < %s' % dump_file)


def local_clone():
    dump_file = execute(backupdb)
    execute(restore_localdb, dump_file)

I cannot understand the prompt for potato password on dropdb potato sentence, when It is supposed that the system have been switched to postgres user.

I've tried with sudo, but the two sentences inside the switch in restore_localdb must be executed as postgres.

I've tried also with env.user and nothing. Also with:

sudo('dropdb potato', user='postgres')

with same result.

local command is not useful for me because I need to switch between users and I can't do it with local.

I only want to emulate these bash commands:

> sudo su - postgres
# I'll write the password for root
> dropdb potato
# The database is finally dropped without any more prompts

Any idea?

0

There are 0 best solutions below