python - 使用 fabric 如何为 psql 提供密码?

标签 python postgresql amazon-web-services fabric amazon-rds

我希望能够从远程计算机登录到我的 AWS postgres 数据库。我正在使用以下 Fabric 脚本:

import sys
from fabric.api import env, run, abort

env.port = 123
env.use_ssh_config = True

def setuser(user):
    """Sets the ssh user for the fabric script"""
    env.user = user
    env.password = 'mypassword'

def setenv(server):
    """Sets the environment for the fabric script"""
    env.hosts = ['staging']

def sync():
    # log into AWS server
    run("psql --host=staging.xxx.rds.amazonaws.com --username=x_user  --port=5432 --password --dbname=x_database")
    run("mypassword")

我使用以下命令运行此 Fabric 脚本:

fab -f sync_staging.py sync --password=mypassword

这让我登录到远程机器,运行 run("psql .... 然后提示我输入密码:

[stage] out: Password for user x_user: 

有什么方法可以让我提供密码(或响应提示)以便它自动登录?

最佳答案

我知道有两种方法可以解决这个问题:

如果您需要在远程主机上设置环境变量,请使用 with shell_env(PGPASSWORD='mypassword'),Fabric 文档在这里:fabric.context_managers.shell_env

希望它能解决您的问题。

关于python - 使用 fabric 如何为 psql 提供密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21266626/

相关文章:

python - Pandas to_sql 给出带有时区感知列的 ValueError

postgresql - 为什么 postgresql "disrespects"哈希索引?

amazon-web-services - 在 aws cloudwatch 中过滤日志流

amazon-web-services - 如何构建 SNS 过滤策略以匹配部分 S3 对象键

python - 如果字典有多个值,如何使用 try except ?

sql - 如何将 PostgreSQL 行扩展为多行以更新 "custom fields"?

python - 在 Jupyter Notebook 中使用 Ctypes

amazon-web-services - [fog][WARNING] 无法获取凭据:没有到主机的路由 - connect(2) -(Errno::EHOSTUNREACH)

python - Leetcode 删除数组中某个值的实例的问题

python - 没有模型形式的django模型实例