【问题标题】:Using fabric how can I supply a password for psql?使用fabric如何为psql提供密码?
【发布时间】:2014-02-11 13:10:00
【问题描述】:

我希望能够从远程计算机登录到我的 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: 

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

【问题讨论】:

  • 您是否尝试在 pg_hba.conf 文件中将数据库配置设置为trust?我不确定,但我认为 psql 不要求输入密码是必需的。
  • 我无法编辑 pg_hba.conf 文件,因为 AWS 不允许这样做。
  • 我刚刚在RDS控制台上查看,在“参数组”下,可以更改一堆参数,并且有一个“可修改”标志。 hba.conf 文件不可修改,因此可能无法允许 IP 无密码访问。

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


【解决方案1】:

据我所知有两种解决方法:

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

希望它能解决你的问题。

【讨论】:

  • 感谢您的建议。刚刚尝试使用 `run('export PGPASSWORD=mypassword') 但这不起作用。
  • 太棒了! with context_managers.shell_env(PGPASSWORD='mypassword') 工作。
猜你喜欢
  • 2013-05-31
  • 1970-01-01
  • 2013-01-06
  • 1970-01-01
  • 2013-02-19
  • 2015-05-30
  • 2020-06-12
  • 2016-01-10
  • 2018-02-11
相关资源
最近更新 更多