PostgreSQL备份脚本无法访问.pgpass

时间:2018-06-05 08:22:23

标签: postgresql postgresql-9.6

我已经创建了一个备份脚本,我希望在每晚的cron作业中运行,但每当我运行./pg_backup.sh时,我都会提示我提供postgres用户密码,尽管我有一个pgpass.config文件到位。

这是我的pg_backup.config

##############################
## POSTGRESQL BACKUP CONFIG ##
##############################

# Optional system user to run backups as.  If the user the script is running as doesn't match this
# the script terminates.  Leave blank to skip check.
BACKUP_USER=

# Optional hostname to adhere to pg_hba policies.  Will default to "localhost" if none specified.
HOSTNAME=

# Optional username to connect to database as.  Will default to "postgres" if none specified.
USERNAME=

# This dir will be created if it doesn't exist.  This must be writable by the user the script is
# running as.
BACKUP_DIR=/var/lib/pgsql/9.6/backups/

# List of strings to match against in database name, separated by space or comma, for which we only
# wish to keep a backup of the schema, not the data. Any database names which contain any of these
# values will be considered candidates. (e.g. "system_log" will match "dev_system_log_2010-01")
SCHEMA_ONLY_LIST=""

# Will produce a custom-format backup if set to "yes"
ENABLE_CUSTOM_BACKUPS=no

# Will produce a gzipped plain-format backup if set to "yes"
ENABLE_PLAIN_BACKUPS=yes

# Will produce gzipped sql file containing the cluster globals, like users and passwords, if set to "yes"
ENABLE_GLOBALS_BACKUPS=no


#### SETTINGS FOR ROTATED BACKUPS ####

# Which day to take the weekly backup from (1-7 = Monday-Sunday)
DAY_OF_WEEK_TO_KEEP=6

# Number of days to keep daily backups
DAYS_TO_KEEP=5

# How many weeks to keep weekly backups
WEEKS_TO_KEEP=1

######################################

请注意,当我输入提示密码时,会按预期创建备份。我遇到的主要问题是,如果需要提示输入postgres密码,则此脚本不会在cron作业中运行。 有没有人知道解决方案或其他解决方案

2 个答案:

答案 0 :(得分:0)

解决此问题的最简单方法是以postgres用户身份运行脚本 - 大多数Unices的Postgres数据库的标准用户:

sudo -iu postgres /path/to/pg_backup.sh

默认情况下,此用户是超级用户授权。

答案 1 :(得分:0)

如果有人仍在寻找解决方案,则有两种选择:

a。)只需删除-h "$HOSTNAME"

b。)创建一个.pgpass文件并添加标志-w,这意味着:

从不发出密码提示。如果服务器要求密码验证,而其他方式(例如.pgpass文件)无法使用密码,则连接尝试将失败。此选项在没有用户输入密码的批处理作业和脚本中很有用。