解决密码问题

postgresql为开发者提供两种密码模式,-W 强制使用(--password) -w 无密码模式(--no-password)

  • -W 会在执行时主动提示输入密码
  • -w 与pgpass.conf配合使用,执行时会自动获取连接配置,适合脚本

pgpass.conf

windows : %APPDATA%\postgresql\pgpass.conf

# 格式 host:port:dbname:user:password
# 若password中含有 : ,使用 \: 进行转义

192.168.9.222:5432:*:postgres:xxxxxx
localhost:5432:*:postgres:xxxxxx

脚本


# 备份单个数据库
pg_dump -h 192.168.9.222 -p 5432 -U postgres -w -F c -f C:\Users\Administrator\Desktop\backup\foo.backup db_test

# 备份所有数据库
pg_dumpall -h 192.168.9.222 -p 5432 -U postgres -w --encoding=UTF8 -f C:\Users\Administrator\Desktop\backup\foo.backup

# 还原
.\psql.exe -h localhost -p 5432 -U postgres -w -f C:\Users\Administrator\Desktop\backup\foo.backup

# 查看所有参数
<commond> --help