pg_restore 忽略 .pgpass 和 PGPASSWORD 环境变量

pg_restore ignores .pgpass and PGPASSWORD environment variable

我想在没有密码提示的情况下使用 pg_restore 导入备份。 我尝试了几个选项,但在我 运行 脚本之后,它总是要求输入密码。 pg_dump 正在工作,但 pg_restore 不工作。如果我输入我的密码,我可以 运行 pg_restore 命令,但我想要一个无密码的命令,或者至少我不想输入我的密码,因为脚本必须在没有用户交互的情况下工作。

什么对我有用:

 PGPASSWORD=xyz pg_dump -h localhost -U user -Fc database > ~/dump_prod.pgsql

什么不起作用

1.)

PGPASSWORD=xyz pg_restore -h localhost -d database -U user -W --clean --no-owner ~/dump_prod.pgsql

2.)

pg_restore --dbname=postgresql://user:pass@localhost:5432/db -W --clean --no-owner ~/dump_prod.pgsql

3.)

touch ~/.pgpass
echo "*:*:*:*:password > ~/.pgpass
chmod 0600 ~/.pgpass
pg_restore -h localhost -d db -U user -W --clean --no-owner ~/dump_prod.pgsql

有什么想法吗?

此致

根据 doc-W 将提示输入密码。 -w不会

-w
--no-password

Never issue a password prompt. If the server requires password authentication and a password is not available by other means such as a .pgpass file, the connection attempt will fail. This option can be useful in batch jobs and scripts where no user is present to enter a password.

-W
--password

Force pg_restore to prompt for a password before connecting to a database.

.pgpass 文件对我有用。

我的设置: 我正在将数据库恢复到 postgres 实例 运行ning 作为 Docker 容器。

postgres 实例 运行 使用命令:

docker run --name postgres_db -p 5432:5432 -e POSTGRES_PASSWORD=admin -d postgres:9.6

pg_restore 存在,但它可作为单独的工具在 docker 安装程序之外使用。

~/.pgpass 文件条目如下所示:

localhost:5432:db_name:user:password


在哪里:

db_name 是要恢复的目标数据库名称。

user 是将要执行还原的用户的名称 - 在我的例子中是管理员用户。即 postgres postgres 实例中的用户。

密码 - 管理员用户的密码。即 admin