123456789101112131415161718192021222324252627 |
- param (
- [string]$backup_dir = $(throw "请提供备份目录作为第一个参数")
- )
- # 设置环境变量
- $env:PGPASSWORD = 'postgres'
- $host_ = '127.0.0.1'
- $port = '5432'
- $username = 'postgres'
- $dbname = 'financialdb'
- $jobs = '16'
- # 检查数据库是否存在
- $checkDbExists = psql -U $username -tA -c "SELECT 1 FROM pg_database WHERE datname='$dbname'" | Out-String
- if ($checkDbExists.Trim() -ne "1") {
- Write-Output "数据库 $dbname 不存在,正在创建..."
- createdb -U $username $dbname
- } else {
- Write-Output "数据库 $dbname 已存在。"
- }
- # 执行 pg_restore
- Write-Output "开始从 $backup_dir 恢复数据库 $dbname..."
- pg_restore -O -c -v -h $host_ -p $port -U $username -d $dbname -Fd -j $jobs $backup_dir
|