您好我编写简单的批处理脚本来备份postgeSQL数据库,但是我发现一个奇怪的问题是 pg_dump 命令是否可以指定密码?
有批处理脚本:
REM script to backup PostgresSQL databases
@ECHO off
FOR /f "tokens=1-4 delims=/ " %%i IN ("%date%") DO (
SET dow=%%i
SET month=%%j
SET day=%%k
SET year=%%l
)
SET datestr=%month%_%day%_%year%
SET db1=opennms
SET db2=postgres
SET db3=sr_preproduction
REM SET db4=sr_production
ECHO datestr is %datestr%
SET BACKUP_FILE1=D:\%db1%_%datestr%.sql
SET FIlLENAME1=%db1%_%datestr%.sql
SET BACKUP_FILE2=D:\%db2%_%datestr%.sql
SET FIlLENAME2=%db2%_%datestr%.sql
SET BACKUP_FILE3=D:\%db3%_%datestr%.sql
SET FIlLENAME3=%db3%_%datestr%.sql
SET BACKUP_FILE4=D:\%db14%_%datestr%.sql
SET FIlLENAME4=%db4%_%datestr%.sql
ECHO Backup file name is %FIlLENAME1% , %FIlLENAME2% , %FIlLENAME3% , %FIlLENAME4%
ECHO off
pg_dump -U postgres -h localhost -p 5432 %db1% > %BACKUP_FILE1%
pg_dump -U postgres -h localhost -p 5432 %db2% > %BACKUP_FILE2%
pg_dump -U postgres -h localhost -p 5432 %db3% > %BACKUP_FILE3%
REM pg_dump -U postgres -h localhost -p 5432 %db4% > %BACKUP_FILE4%
ECHO DONE !
请给我建议
问Mick
答案 0 :(得分:2)
已修改以反映评论中的信息
没有选项可以提供密码表单命令行。您需要包含-w
或--no-password
开关,并创建一个包含连接密码的文件。它是
hostname:port:database:username:password
默认情况下,此文件将从%APPDATA%\postgresql\pgpass.conf
重新启动,但可以更改其位置,将环境变量PGPASSFILE
设置为要使用的文件的路径。