【问题标题】:Issue with manually giving password every time with psql每次使用 psql 手动输入密码的问题
【发布时间】:2019-09-18 07:10:57
【问题描述】:

我们正在尝试通过 COPY 命令使用 postgresql 的 psql 将数据从一个 Amazon RDS 数据库迁移到 Amazon Aurora Serverless 数据库。当我从 EC2 实例运行该脚本时,它运行良好,但我需要手动为 rdswizardpostgres 每次迭代提供密码。我只想在psql 命令中提供密码。如何在psql命令中不每次都手动输入密码?

allSites=(3 5 9 11 29 30 31 32 33 34 37 38 39 40 41 45 46 47 48)

for i in "${allSites[@]}"
do
   psql \
    -X \
    -U rdswizard \
    -h my_rds_host_url_goes_here \
    -d wizard \
    -c "\\copy (select site_id,name,phone from client_${i} where date(create_date) > '2019-09-11' LIMIT 100) to stdout" \
| \
psql \
    -X \
    -U postgres \
    -h my_aurora_serverless_host_url_goes_here \
    -d wizard \
    -c "\\copy client_${i}(site_id,name,phone) from stdin"
done

我的两个数据库主机都在远程服务器上而不是在本地机器上

【问题讨论】:

    标签: bash postgresql amazon-rds psql aws-aurora-serverless


    【解决方案1】:

    您可以向~/.pgpass 文件添加详细信息,以避免经常需要输入密码。确保为文件提供-rw-------(0600) 权限。

    此文件应包含以下格式的行:

    hostname:port:database:username:password
    

    将使用第一行中与当前连接参数匹配的密码字段。参考official documentation

    【讨论】:

    • 那么对于两个主机我需要在 pgpass 文件上使用两行?
    • 正确。将根据您的连接(主机名、数据库、用户名)自动选择相应的密码。
    • 让我今天在办公室检查一下,然后我会通知你
    • 您的解决方案工作正常,我只是做了一个小改动,PGPASSFILE=~/.pgpass 在我的psql 命令之前添加。 请看这里: newfivefour.com/postgresql-pgpass-password-file.html
    猜你喜欢
    • 2018-08-12
    • 2019-07-27
    • 2017-09-24
    • 2022-01-25
    • 1970-01-01
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多