# Execute PostgreSQL fix remotely via SSH $ErrorActionPreference = "Continue" Write-Host "========================================" -ForegroundColor Cyan Write-Host " Fixing PostgreSQL Port Configuration" -ForegroundColor Cyan Write-Host "========================================" -ForegroundColor Cyan Write-Host "" $REMOTE_HOST = "10.1.0.254" $REMOTE_USER = "ubuntu" $PASSWORD = "DC1qaz2wsx" # Create the command as a single string $commands = @" echo '[1/4] Stopping old PostgreSQL container...' docker stop postgres 2>/dev/null docker rm postgres 2>/dev/null echo '' echo '[2/4] Starting new container with external access...' docker run -d \ --name postgres \ --restart unless-stopped \ -e POSTGRES_PASSWORD="$PASSWORD" \ -e TZ=Asia/Taipei \ -p 0.0.0.0:5432:5432 \ -v postgres-data:/var/lib/postgresql/data \ postgres:16 echo '' echo '[3/4] Waiting for PostgreSQL...' sleep 8 echo '' echo '[4/4] Verifying...' docker ps | grep postgres docker port postgres docker exec postgres psql -U hr_user -d hr_portal -c 'SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='\''public'\'' AND table_type='\''BASE TABLE'\'';' 2>/dev/null || echo 'Database check will be done after connection test' echo '' echo '==========================================' echo 'PostgreSQL reconfiguration complete!' echo '==========================================' "@ Write-Host "Connecting to Ubuntu Server..." -ForegroundColor Yellow Write-Host "" # Try using plink if available $plinkPath = Get-Command plink -ErrorAction SilentlyContinue if ($plinkPath) { Write-Host "Using plink for connection..." -ForegroundColor Gray $commands | plink -ssh -batch -pw $PASSWORD "${REMOTE_USER}@${REMOTE_HOST}" } else { # Fallback to regular ssh Write-Host "Using ssh for connection..." -ForegroundColor Gray Write-Host "You may need to enter password: $PASSWORD" -ForegroundColor Yellow Write-Host "" # Save commands to temp file $tempFile = "$env:TEMP\fix-postgres-commands.sh" $commands | Out-File -FilePath $tempFile -Encoding UTF8 # Try to execute via ssh with heredoc $sshCommand = "bash -s" Get-Content $tempFile | ssh "${REMOTE_USER}@${REMOTE_HOST}" $sshCommand } $exitCode = $LASTEXITCODE Write-Host "" if ($exitCode -eq 0) { Write-Host "========================================" -ForegroundColor Green Write-Host " Success!" -ForegroundColor Green Write-Host "========================================" -ForegroundColor Green Write-Host "" Write-Host "PostgreSQL is now accessible from:" -ForegroundColor White Write-Host " Host: 10.1.0.254" -ForegroundColor Cyan Write-Host " Port: 5432" -ForegroundColor Cyan Write-Host "" Write-Host "Testing connection from Windows..." -ForegroundColor Yellow Write-Host "" # Test connection Start-Sleep -Seconds 3 Write-Host "Running database connection test..." -ForegroundColor Yellow cd "W:\DevOps-Workspace\hr-portal\backend" python test_db_connection.py } else { Write-Host "========================================" -ForegroundColor Red Write-Host " Error occurred" -ForegroundColor Red Write-Host "========================================" -ForegroundColor Red Write-Host "" Write-Host "Exit code: $exitCode" -ForegroundColor Yellow Write-Host "" Write-Host "Please check the output above for errors." -ForegroundColor Yellow } Write-Host ""