pg_dump: [archiver (db)] 与数据库的连接失败:致命:用户“postgres”的对等身份验证失败

Dra*_*ace 7 postgresql psql database-backup

我正在尝试将我的 Postgres 数据库备份到另一台服务器,但我一直被拒绝访问。

我的 pg_hba.conf 文件如下所示:

# DO NOT DISABLE!
# If you change this first entry you will need to make sure that the
# database superuser can access the database using some other method.
# Noninteractive access to all databases is required during automatic
# maintenance (custom daily cronjobs, replication, and similar tasks).
#
# Database administrative login by Unix domain socket
local   all             postgres                                peer

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                peer
#host    replication     postgres        127.0.0.1/32            md5
#host    replication     postgres        ::1/128                 md5
Run Code Online (Sandbox Code Playgroud)

如果我将 postgres 设置从 PEER 更改为 TRUST,我可以备份代码,但网站无法再访问数据库(关于 SSL 错误,我知道这不是问题)。

当我保持原样时,PEER,服务器工作 - php 可以访问数据库 - 但我在尝试运行备份时出错:

root> pg_dump -U postgres 表名> test.sql

.pg_dump: [archiver (db)] 到数据库“tablename”的连接失败:致命:用户“postgres”的对等身份验证失败。

PHP Apache 网站使用适当的用户名和密码。

备份脚本如下所示: pg_dump -U postgres tablename > test.sql

我怎样才能同时满足网站和备份脚本,以便我可以同时运行两者?

整个脚本尝试是这样的:ssh SERVER "pg_dump -U postgres tablename| gzip -c" > /backup/sqlbackup.sql.gz

取自服务器故障:ssh remote command-fu

kri*_*sFR 12

如果您的 PHP Apache 网站使用peer,请保留它,让我们处理它。

1.第一种方法:

由于身份验证是基于对等的,您必须使用-h选项指定主机名(通常是本地主机):

ssh server "pg_dump -h localhost -U postgres | gzip -c" >/backup/sqlbackup.sql.gz
Run Code Online (Sandbox Code Playgroud)

这里的主要问题是系统会提示您输入 postgres 用户密码。这是备份自动化的问题。

可以参考这个避免提示输入密码。但我会描述:

  1. 创建一个名为.pgpassin的隐藏文件$HOME
  2. 执行一个 chmod 600 .pgpass
  3. 编辑文件.pgpass并添加这一行:localhost:5432:postgres:postgres:password

2. 第二种方法:

您可以为备份添加特定角色并信任它。

在 Postgres 中创建备份角色:

CREATE ROLE backup WITH LOGIN SUPERUSER PASSWORD 'password'
Run Code Online (Sandbox Code Playgroud)

编辑文件pg_hba.conf,添加角色backuptrust它:

# Database administrative login by Unix domain socket
local   all             postgres                                peer
local   all             backup                                  trust
Run Code Online (Sandbox Code Playgroud)

重新开始 postgresql

然后您应该能够使用以下内容运行备份:

ssh server "pg_dump -U backup postgres | gzip -c" >/backup/sqlbackup.sql.gz
Run Code Online (Sandbox Code Playgroud)